When does the JS engine create a Garbage Collection Root?

A bit of background

I am developing a large-scale JS application with ExtJS3. At run time, the user can open and close numerous widgets, which may increase memory usage. I fixed a lot of memory holes using the Chrome heap analyzer , but in some cases I just can't find the culprit. The heap analyzer shows something like GCRoot[1234]->store.items , and I cannot find the code section to which the repository will link.

Question

What are the exact execution conditions under which a V8 (or any other JS engine) will create a new garbage collector root? Are there specific code templates (closure, eval, event listes, ...) that force it?

+4
source share
2 answers

GC Roots is a special group of objects that are used by the garbage collector as a starting point to determine which objects to collect garbage from. A β€œroot” is simply an object that the garbage collector assumes is available by default , which then has links traced to find all other current objects that are reachable. Any object inaccessible by any link, the chain of any of the root objects is considered unreachable and will ultimately be destroyed by the garbage collector. In V8, the roots consist of objects in the current call stack (i.e., local variables and parameters of the current executable function), the active handle of V8 by areas, global descriptors, and objects in the compilation cache .

via http://zetafleet.com/blog/google-chromes-heap-profiler-and-memory-timeline

Q: What contains GC roots?

A: A lot of things:

  • built-in maps of objects;
  • table of symbols;
  • VM thread stacks
  • compilation cache;
  • management areas
  • global descriptors.

via http://code.google.com/chrome/devtools/docs/heap-profiling.html

+4
source

I'm not a javascript memory management specialist, but from what I know:

Make sure all event handlers are cleared. Any event handlers attached to an object / element will make it difficult for the GC to do its job.

Use prototype inheritance whenever possible: (http://www.crockford.com/javascript/private.html). It allows objects to refer to the same method in memory instead of re-creating one for each object - saving memory and increasing performance for the javascript engine.

Indicate any variables / properties that are not used.

Hope this helps

+1
source

Source: https://habr.com/ru/post/1401961/


All Articles