Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

References to SVG/DOM elements not Garbage Collected

Has anyone one else noticed that when using document.getElementById across many elements different elements in the SVG, the JS memory just keeps increasing and never returns to the previous level?

 

I have tried specifically nulling out references used inside functions at the end of the function, which should not be required really as a reference should be garbage collected once the function goes out of scope and the variable is no longer reachable, but still no difference.

 

I tried declaring a single global variable which is then used inside a function to access an element but it makes no difference.

 

It seems that no matter what is done, every usage of document.getElementById uses some JS memory which is never returned. I have had it running for hours and can confirm the the memory is never released.

I have also tried refactoring the code to use element.firstChild and element.nextSibling but every usage of those also increases memory used.

 

Interesting to note that after around 300 elements have been accessed, which happens over the course of 5 mins, the amount of memory used is almost as much as is used if I created a global array up front containing all of the elements that I need by using getElementsByTagName.

 

I also noticed that the available methods for getting elements is limited:
https://community.fitbit.com/t5/SDK-Development/Get-child-of-DOM-element/td-p/3374037
I was hoping there would be childNodes[] method.

 

It looks like this an issue with the garbage collection for element references, I have narrowed it down to the line of code which grabs the element and then changes the class and style.opacity of the element, with those lines commented out, the memory usage does not increase.

 

Best Answer
0 Votes
2 REPLIES 2

Three thoughts:

  • Element memory is never fully deallocated; you're right about that.
  • JS garbage collection is non-deterministic. The system doesn't try to free a block of memory as soon as it is no longer required. Usually, it waits until the memory seems to be exhausted, then it tries to reclaim what it can. Because of this, you can't just monitor ongoing memory usage to see what is required; you have to watch for the collections.
  • Have a look at children.
Peter McLennan
Gondwana Software
Best Answer
0 Votes

Thanks for that info, good to know that I'm not chasing a ghost with the element memory usage.

 

I have noticed with the garbage collection that it is rather lazy, but I can see memory going down when other references are freed up.

 

Developers will just have to be aware that the baseline memory usage can't be accurately measured until an app been running along enough since startup to have accessed all of the elements that it ever will. It makes it rather hard to safely work close to the memory limits.

Best Answer
0 Votes