Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find non-destroyed but GC'ed Javascript objects in Chrome?

I have an application with some objects (of type BaseTexture) on which an explicit destroy function should be called when they aren't of any more use. Otherwise they might leak some memory. This should be done before they are garbage collected (and obviously only can be done then), but it doesn't always happen.

In Java I would detect and log this using finalize, but such a thing does not exist in Javascript.

Can I detect this in Chrome (or in another browser)? I don't really care if it's buggy, requires flags, can only log a simple message, etc, as long as it works during development. The fact that a BaseTexture has been destroyed can be retrieved from its source property.

like image 767
Bart van Heukelom Avatar asked Jul 06 '15 15:07

Bart van Heukelom


1 Answers

If the purpose of this is to check for memory leaks, then why cant you just run a chrome profile instead?

The Object allocation tracker can be used to find memory leaks at runtime, also the heap profiler can analyze memory graphs and compare snapshots to discover what objects are not being cleaned up by the gc.

Also the timeline memory view can help identify if you are forcing garbage collection too often by allocating too often (if thats of interest)

For more info, see: https://developer.chrome.com/devtools/docs/javascript-memory-profiling

Also not sure if its helpful, but if you want to look at memory stats then you can enable memory info in chrome by running with the --enable-memory-info param, then you have access to window vars:

window.performance.memory.jsHeapSizeLimit
window.performance.memory.totalJSHeapSize
window.performance.memory.usedJSHeapSize
like image 84
Marty Avatar answered Sep 27 '22 23:09

Marty