GC Roots -- what are they?

3,319 views
Skip to first unread message

Ramki Gaddipati

unread,
Jan 29, 2012, 5:00:47 PM1/29/12
to Chromium-dev
Hi,

Can you please point me to some info related to what GC Roots mean,
how they are created and managed?
In a heap snapshot of my app, I see that one GC Root is holding on to
several thousands of objects of various kinds, I don't want to be
retained in memory. I am clueless about it.

thanks,
+ramki

Ramki Gaddipati

unread,
Jan 29, 2012, 5:22:53 PM1/29/12
to Chromium-dev
I read about what GC Roots are a typical GC implementation (of Java).
I am interested in how it is done in V8 and how the objects passed to
Webkit methods or other native methods become available for garbage
collection.

thanks,
+ramki

Mikhail Naganov

unread,
Jan 29, 2012, 6:11:05 PM1/29/12
to ramki...@gmail.com, Chromium-dev
Hi Ramki,

Short answer -- you shouldn't be worried by objects that are only retained by V8's GC roots. Assuming that Chrome and V8 don't have bugs (ahem!), they will be released sooner or later. If you are developing a web application, you need to worry only about references that you create yourself, and they are usually traceable to window objects and dom trees.

If you really need to know -- we have recently added some grouping of GC roots into categories in heap profiler. Check out Chrome dev channel or Chrome canary. What some of those group names mean:
 (Isolate) -- the current call stack
 (Handle scope) -- objects from VM heap that are temporarily referenced from the currently running native code
 (Global handles) -- objects from VM heap that are premanently referenced from native code

If you are still interested, you should get a deeper answer on the V8 users list.

--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
   http://groups.google.com/a/chromium.org/group/chromium-dev

Ramki Gaddipati

unread,
Jan 29, 2012, 9:55:57 PM1/29/12
to Chromium-dev
Thanks Mikhail.

It is common to observe in snapshots of my app that GC Root (Global
handles) is holding on to more than 25000 HTML Elements.
Also I notice several thousands of strings, all tracing no retaining
path to the window but the GC root.

The app is expected to run for several days at a stretch. With time
the memory usage is shooting up so much that it leaves no option but
to restart the app (usually in two days). In the heap snapshot I see a
really large number of strings, arrays, objects and closures. Most of
them don't have any retaining path from the window.

Can you help me understand when an object gets added to and deleted
from Global handles?
For example, if I use XMLHTTPRequest, which is a native object and
register event handlers, I assume the event handlers are added to
Global handles. But when do they become available for garbage
collection?

thanks,
+ramki
> > Chromium Developers mailing list: chromium-...@chromium.org

Ilya Tikhonovsky

unread,
Jan 30, 2012, 4:45:37 AM1/30/12
to ramki...@gmail.com, Chromium-dev
Looks like you have a leak in your app. It can be a problem with your js code or with chromium code.

You can try to find a scenario when the leak happens.
For simplifying the process you can do the next steps:

1) please install Chrome from Canary channel. (windows or mac)
2) open your page
3) open DevTools
4) click on a small gear at right down corner of the DevTools window for the settings screen
5) enable 'Show memory counters in Timeline Panel' option
6) close DevTools window and open it again
7) open Timeline Panel
8) click on the 'Toggle DOM counters graph' button in the DevTools window status bar.

you will see graphs with DOM counters at the bottom of Timeline panel.

Now you can start recording the timeline, do an action a few times and look at the counters.
If after this action and garbage collecting the counters don't return to their initial values then these operations have a leak.
Tip: There is a GC button at the status bar in Timeline Panel.

When you find a scenario you can use Summary view with filters or Diff view.

Regards,
Tim.


Chromium Developers mailing list: chromi...@chromium.org
button.png

Mikhail Naganov

unread,
Jan 30, 2012, 6:05:01 AM1/30/12
to ramki...@gmail.com, Chromium-dev
Please follow Ilya's advice on verifying your app for leaks. More
answers inline.

On Mon, Jan 30, 2012 at 02:55, Ramki Gaddipati <ramki...@gmail.com> wrote:
> Thanks Mikhail.
>
> It is common to observe in snapshots of my app that GC Root (Global
> handles) is holding on to more than 25000 HTML Elements.
> Also I notice several thousands of strings, all tracing no retaining
> path to the window but the GC root.
>

That's normal. When you access a DOM node from your JS code, a wrapper
object is created in JS heap, and a weak global handle is created for
the native object. Those handles are harmless. It's about the same
story with strings. Some of them are residing in browser's memory, and
V8 only references them.

What you should look at is whether your app creates leaks detached DOM
trees -- this is what upcoming DOM counters are for.

> The app is expected to run for several days at a stretch. With time
> the memory usage is shooting up so much that it leaves no option but
> to restart the app (usually in two days). In the heap snapshot I see a
> really large number of strings, arrays, objects and closures. Most of
> them don't have any retaining path from the window.
>

What happens if you press the "Collect garbage" button on Timeline panel?

> Can you help me understand when an object gets added to and deleted
> from Global handles?
> For example, if I use XMLHTTPRequest, which is a native object and
> register event handlers, I assume the event handlers are added to
> Global handles. But when do they become available for garbage
> collection?
>

That really depends on the nature of the object. For the XHR object
the agreement should be as described here:
http://www.w3.org/TR/XMLHttpRequest/#garbage-collection

> Chromium Developers mailing list: chromi...@chromium.org

Ramki Gaddipati

unread,
Jan 30, 2012, 11:07:00 AM1/30/12
to Chromium-dev
Thanks for the detailed mail. I will check this.

+ramki

On Jan 30, 3:45 am, Ilya Tikhonovsky <loi...@chromium.org> wrote:
> Looks like you have a leak in your app. It can be a problem with your js
> code or with chromium code.
>
> You can try to find a scenario when the leak happens.
> For simplifying the process you can do the next steps:
>
> 1) please install Chrome from Canary channel. (windows or mac)
> 2) open your page
> 3) open DevTools
> 4) click on a small gear at right down corner of the DevTools window for
> the settings screen
> 5) enable 'Show memory counters in Timeline Panel' option
> 6) close DevTools window and open it again
> 7) open Timeline Panel
> 8) click on the 'Toggle DOM counters graph' button in the DevTools
> window status bar.
>
> you will see graphs with DOM counters at the bottom of Timeline panel.
>
> Now you can start recording the timeline, do an action a few times and look
> at the counters.
> If after this action and garbage collecting the counters don't return to
> their initial values then these operations have a leak.
> Tip: There is a GC button at the status bar in Timeline Panel.
> *
> *
> *When you find a scenario you can use Summary view with filters or Diff
> view.*
>
> Regards,
> Tim.
>  button.png
> 9KViewDownload

Ramki Gaddipati

unread,
Jan 30, 2012, 11:14:20 AM1/30/12
to Chromium-dev
Mikhail,

In the W3C spec for XMLHttpRequest (http://www.w3.org/TR/
XMLHttpRequest/#garbage-collection) I see the following:
>>> It has one or more event listeners registered whose type is readystatechange, progress, abort, error, load, timeout, or loadend.

Does this mean, the XHR object is not garbage collected after the
event is delivered and say the event handler goes out of scope from
the Javascript code?
Do I have to explicitly remove event listeners to make the XHR object
available for garbage collection?

The statement in the spec sounds so. But I hope it is not the intent.

Also if a I register certain event handlers on DOM node and if the DOM
node is detached and is not referred to by any object in the
Javascript world (including the event handler), will the DOM node be
garbage collected?

thanks,
+ramki


On Jan 30, 5:05 am, Mikhail Naganov <mnaga...@chromium.org> wrote:
> Please follow Ilya's advice on verifying your app for leaks. More
> answers inline.
>

Ramki Gaddipati

unread,
Jan 30, 2012, 12:51:11 PM1/30/12
to Chromium-dev


On Jan 30, 3:45 am, Ilya Tikhonovsky <loi...@chromium.org> wrote:
> Looks like you have a leak in your app. It can be a problem with your js
> code or with chromium code.
>
> You can try to find a scenario when the leak happens.
> For simplifying the process you can do the next steps:
>
> 1) please install Chrome from Canary channel. (windows or mac)
> 2) open your page
> 3) open DevTools
> 4) click on a small gear at right down corner of the DevTools window for
> the settings screen
> 5) enable 'Show memory counters in Timeline Panel' option

The latest Canary (18.0.1023.0 canary) doesn't give me this option.
Can you point me to some build of Chromium which might have this
option?


> 6) close DevTools window and open it again
> 7) open Timeline Panel
> 8) click on the 'Toggle DOM counters graph' button in the DevTools
> window status bar.
>
> you will see graphs with DOM counters at the bottom of Timeline panel.
>
> Now you can start recording the timeline, do an action a few times and look
> at the counters.
> If after this action and garbage collecting the counters don't return to
> their initial values then these operations have a leak.
> Tip: There is a GC button at the status bar in Timeline Panel.
> *
> *
> *When you find a scenario you can use Summary view with filters or Diff
> view.*
>
> Regards,
> Tim.
>  button.png
> 9KViewDownload

Ilya Tikhonovsky

unread,
Jan 31, 2012, 2:29:38 AM1/31/12
to ramki...@gmail.com, Chromium-dev
I'm sorry, I missed that you have to run chrome with --enable-devtools-experiments flag.

Regards,
Tim.



--
Chromium Developers mailing list: chromi...@chromium.org

Mikhail Naganov

unread,
Jan 31, 2012, 11:45:14 AM1/31/12
to ramki...@gmail.com, Chromium-dev
On Mon, Jan 30, 2012 at 16:14, Ramki Gaddipati <ramki...@gmail.com> wrote:
>
> Mikhail,
>
> In the W3C spec for XMLHttpRequest (http://www.w3.org/TR/
> XMLHttpRequest/#garbage-collection) I see the following:
> >>> It has one or more event listeners registered whose type is readystatechange, progress, abort, error, load, timeout, or loadend.
>
> Does this mean, the XHR object is not garbage collected after the
> event is delivered and say the event handler goes out of scope from
> the Javascript code?
> Do I have to explicitly remove event listeners to make the XHR object
> available for garbage collection?
>
> The statement in the spec sounds so. But I hope it is not the intent.
>

The spec says: "... its state is HEADERS_RECEIVED, or its state is
LOADING, and ... has one or more event listeners ...".

This is a reasonable condition. If your code is interested in the
result, and the activity is still going, the XHR object will not be
garbage-collected, even if it's not referenced.
When loading has finished, it doesn't matter whether the object has
event listeners, or not. It will be GCed once you erase all references
to it.
What you might need to do is to make sure to call the 'abort()' method
if you are not sure whether the object has finished its activity.

>
> Also if a I register certain event handlers on DOM node and if the DOM
> node is detached and is not referred to by any object in the
> Javascript world (including the event handler), will the DOM node be
> garbage collected?
>

Sure. Consider this simple example:

<html>
<head>
<title>Event handler test</title>
<script type="text/javascript">
function createWrapper() {
nodeWrapper = document.getElementById("a");
nodeWrapper.onclick = detachNode;
}
function detachNode() {
document.body.removeChild(nodeWrapper);
nodeWrapper = null;
}
</script>
</head>
<body onload="createWrapper()">
<input id="a" type="button" value="A" />
</body>
</html>

If you take a heap snapshot before pressing a button, then after, and
then compare these two snapshots, you'll notice a deleted
"HTMLInputElement" in the diff.

> Chromium Developers mailing list: chromi...@chromium.org

Titansoftime

unread,
Jun 13, 2015, 9:41:33 PM6/13/15
to chromi...@chromium.org, ramki...@gmail.com
I know this is an old thread but what i found is still true. Any object you log in your dev console will be added to this allocation. As soon as i commented out the console log, things were fine.
Reply all
Reply to author
Forward
0 new messages