A little background
I'm developing a large-scale JS app with ExtJS3. At runtime, the user may open and close numerous widgets, thus possibly increasing memory usage. I fixed many memory holes using Chrome's heap analyzer, but in certain cases I just can't find the culprit. The heap analyzer shows something like GCRoot[1234]->store.items
, and I can't find the code section where the store would be referenced.
The question
What's the exact runtime conditons under which V8 (or any other JS engine) would create a new garbage collector root? Are there certain code patterns (closure, eval, event listetes, ...) that force it?
A little background
I'm developing a large-scale JS app with ExtJS3. At runtime, the user may open and close numerous widgets, thus possibly increasing memory usage. I fixed many memory holes using Chrome's heap analyzer, but in certain cases I just can't find the culprit. The heap analyzer shows something like GCRoot[1234]->store.items
, and I can't find the code section where the store would be referenced.
The question
What's the exact runtime conditons under which V8 (or any other JS engine) would create a new garbage collector root? Are there certain code patterns (closure, eval, event listetes, ...) that force it?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 17, 2012 at 8:08 user123444555621user123444555621 153k27 gold badges117 silver badges126 bronze badges 4- I don't believe this could be done via JavaScript in V8, this is from Chrome V8 Developer section: An object is considered garbage if it is inaccessible from JavaScript and there are no handles that refer to it. From time to time the garbage collector removes all objects considered to be garbage developers.google./v8/embed#handles – Kamyar Nazeri Commented Mar 17, 2012 at 8:23
-
@Kamyar There are certainly ways of "helping" the garbage collector by writing code with reference cleanup. For example,
var temp=...; /* use temp */; temp = null;
This will make sure thattemp
does not pollute the associated scope. So I wonder if the opposite is true: Can we intentionally create a GC root by JS user code? – user123444555621 Commented Mar 18, 2012 at 8:22 - Correct. you could reference a null variable or use delete keyword to remove data from stack, however you can not be sure when the GC would start collecting objects with no handle, unlike .Net or Java which you could force the GC to start collecting orphan objects and reclaim inaccessible memory, I believe Chrome designers chose not to let web developers to start mess with GC. That's my two cents though, there might be some undocumented ways to do so! – Kamyar Nazeri Commented Mar 18, 2012 at 8:32
- Guess I could find those triggers by working through the source code, but I don't have time for that, and my C++ knowledge is pretty average ;) – user123444555621 Commented Mar 18, 2012 at 18:12
2 Answers
Reset to default 7GC roots are the special group of objects that are used by the garbage collector as a starting point to determine which objects are eligible for garbage collection. A “root” is simply an object that the garbage collector assumes is reachable by default, which then has its references traced in order to find all other current objects that are reachable. Any object that is not reachable through any reference chain of any of the root objects is considered unreachable and will eventually be destroyed by the garbage collector. In V8, roots consist of objects in the current call stack (i.e. local variables and parameters of the currently executing function), active V8 handle scopes, global handles, and objects in the pilation cache.
via http://zetafleet./blog/google-chromes-heap-profiler-and-memory-timeline
Q: What prises GC roots?
A: Many things:
- built-in object maps;
- symbol table;
- stacks of VM threads;
- pilation cache;
- handle scopes;
- global handles.
via http://code.google./chrome/devtools/docs/heap-profiling.html
I'm no expert on javascript memory management, but from what I know:
Ensure that all your event handlers are being cleaned up. Any event handlers attached to an object/element will make it difficult for the GC to do its job
Use prototypal inheritance whenever possible: (http://www.crockford./javascript/private.html). It allows objects to reference the same method in memory instead of recreating one for each object - saving memory and increasing performance for the javascript engine.
Null any variables/properties that are not used.
Hope this helps