Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log what is being garbage collected in my javascript code?

I've built an application that wastes 40% of its time collecting garbage, and I'm at my wits' end trying to find out where it is coming from. I've corralled any possible problems in my own code, yet it persists. I'm beginning to suspect some third party code of being the problem, and I'd like to know if I can somehow track down what is being garbage collected. If there's a Chrome-specific answer, that'd be great, but I'll take anything at this point.

like image 250
shino Avatar asked May 06 '13 18:05

shino


People also ask

How do you know if an object is garbage collected?

You normally can't tell whether an object has been garbage collected by using some reference to the object–because once you have a reference to the object, it won't be garbage collected. You can instead create a weak reference to an object using the WeakReference object.

Is there garbage collection in JavaScript?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

How does JavaScript handle garbage collection?

JavaScript automatically collects the information of the unmercenary memory blocks and removes them from the memory. The garbage collector searches for reachability from the root object to determine whether an object will be used in the future or not.

What is garbage value in JavaScript?

Reference Counting Garbage Collection in JavaScript It determines whether the value still has any object referencing it. If the object has zero references pointing to it, that object is said to be “garbage”. The garbage collector detects these objects and releases them from memory.


1 Answers

In Chrome I would start with following:

  • DevTools > Profile
  • Take Heap Snapshot
  • Working with the application
  • Take Heap Snapshot again
  • Inspect the second snapshot in the comparision mode

Following links may be useful:

  • http://addyosmani.com/blog/performance-optimisation-with-timeline-profiles/
  • http://rein.pk/using-the-heap-profiler-in-chrome-dev-tools/
like image 141
Oleg Kurbatov Avatar answered Oct 23 '22 23:10

Oleg Kurbatov