ProfileSVG.jl icon indicating copy to clipboard operation
ProfileSVG.jl copied to clipboard

Potential risk of memory leaks

Open kimikage opened this issue 4 years ago • 1 comments

When using ProfileSVG on Jupyter etc., you can remove <SVG> from the DOM tree by clearing the output cell. But the event handlers may not be collected by the GC. Alternatively, the event handlers may prevent some parts of the <SVG> from being collected by the GC.

This is an essential problem, and it is difficult to solve the problem fundamentally, but I think we can reduce the effects of memory leaks.

~The current implementation stores the references to <rect> and <text> nodes in fig,~ https://github.com/kimikage/ProfileSVG.jl/blob/8ea09a8dce3879f8675bb121fbbde102e0d879bf/src/svgwriter.jl#L55-L63 ~and the event handlers refers them.~ https://github.com/kimikage/ProfileSVG.jl/blob/8ea09a8dce3879f8675bb121fbbde102e0d879bf/src/svgwriter.jl#L74-L98

The current implementation should work, but there may be memory issues.

Edit: The issue of holding the references to nodes was slightly improved in PR #26. However, the memory leaks will not be fixed unless we remove all unnecessary references.

kimikage avatar May 02 '20 02:05 kimikage

The simplest solution might be:

let svg_node = something // reference to the SVG node
let handler = setInterval(() => {
	if(svg_node.closest("body") == null) {
		// the node got removed from `body`, time to remove our listeners!
		window.removeEventListener("asdf", reference_to_listener_function);
		// etc

		clearInterval(handler)
	}
}, 3000)

fonsp avatar Feb 18 '22 00:02 fonsp