Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Profiling Javascript in Chrome how do I know I've handled Memory Leaks?

I've been working on a game in for HTML 5 using the canvas tag, and I've build up quite a code base to cover my requirements. I also want to make sure I'm covering up all my memory leaks.

I have doubts I'm doing it correctly because the feedback I'm getting from Chrome's task manager and profiling tools seem to suggest my cleanup is having no effect in the end.

Here's an image so you can see what I mean:

enter image description here

So as you can see, once I do my cleanup memory usage just freezes and doesn't drop. Is this a memory leak?

When I ran my webpage in the Profiler and checked the heap before and after cleanup it does appear to remove all the references to my objects (and they disappear) but my usage only drops by only a few kilobytes leaving about 1.3mb of arrays, strings, and other objects behind. Is it impossible to catch all this or is there something majorly wrong?

Thanks.

like image 713
Ryan Badour Avatar asked Apr 17 '11 05:04

Ryan Badour


People also ask

Can you have memory leaks in JavaScript?

The JavaScript engine allocates memory when you create objects and variables in your application, and it is smart enough to clear out the memory when you no longer need the objects. Memory leaks are caused due to flaws in your logic, and they make way for poor performance in your application.

How do I find a memory leak on a Web application?

Start with metrics such as page load times, HTTP request times, and Core Web Vitals – time to the first byte, first contentful paint. If you use Sematext Experience you'll see a number of other useful metrics for your web applications and websites there. However, metrics themselves are only a part of the whole picture.


3 Answers

At the bottom of the profiler window there is an icon that looks like a trash can, it will force a GC pass.

Hit it and see if it clears up the rest of the memory.

It's possible Chrome/V8 just doesn't think the memory situation is bad enough to require garbage collection to run.

like image 111
david Avatar answered Oct 12 '22 17:10

david


Try chrome://memory-redirect/ (or about:memory, both go to the same place). It'll show you the exact amount of memory being used by each tab/etc, plus the memory used by tags/etc in IE, Opera, Firefox, etc. if you have those open at the same time. The raw numbers should be a little more helpful than just the graph at profiling your memory use for potential leaks.

like image 23
BrianFreud Avatar answered Oct 12 '22 18:10

BrianFreud


You can compare two heap snapshots and see the delta. You can also directly look at the memory snapshot: https://developers.google.com/chrome-developer-tools/docs/heap-profiling-comparison?hl=pt-PT

like image 40
Miguel Ping Avatar answered Oct 12 '22 17:10

Miguel Ping