3

I've recently been reading up on Javascript memory management, and decided to profile a portion of this app that I wrote recently. It makes a jQuery AJAX call every 5 seconds to another PHP page, essentially monitoring the database for changes. No DOM changes occured at any point.

Anyway, I ran the Profiler as well the Timeline thing on Chrome's devtools, and I'm not too sure how to interpret if I have a possible memory leak:

Timeline:

I didn't trigger manual garbage collection (via the button in the toolbar) at any point: Timeline profile

Heap Allocation Profiler:

Ran this for a shorter amount of time, the portion between the 5.0 sec mark to 1.2 min mark doesn't have anything in the "Constructor" panel: Heap allocation profile


If I'm not wrong, the drop in the first image (near the 240000ms mark) is a garbage collection event triggered by the browser.

My question is, why is it that in the second image I do not have any objects in the time that has elapsed (from the 5.0s to 1.2m mark), whilst the JS heap size continues to grow every 5 seconds?

I guess this doesn't indicate a memory leak, but is it true that the heap size continues to grow because I have effectively dereferenced the variables and objects in memory, but my script continues to create new variables every 5 seconds, and all of the dereferenced stuff only gets garbage collected after a few minutes?

More importantly, is this the proper way of handling memory usage? Meaning, is it sufficient to properly dereference your temporary variables, or would I also have to think about triggering garbage collection on my own as well?

P.S. The last question may come across as too broad, I am merely looking for best practices on this topic, as an add-on to the main question, which is to confirm my conclusion to why the heap size continues to grow is correct, as above.

1 Answer 1

1

Don't look at graphs, use snapshots (like in example).

What you need to do to detect leaks is:

  1. Make initial heap snapshot
  2. Wait a second or do some operations that should leak memory
  3. Make second heap snapshot
  4. Use comparison view between two snapshots to see if any elements from first snapshot are being left.

It's fine to have objects in this view, unless it contains some that won't be used anymore.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.