Is there a way to unit test for javascript memory leaks? What I mean is, is there any way to access the heap directly from javascript code to check for detached DOM trees or increased memory usage?
I know you can do this from Chrome Dev Tools, but I'm wondering if there's a way to do it directly from my unit tests, since it seems tedious to write some code, take a heap snapshot, perform a potentially memory leaking operation, take another heap snapshot, and repeat for every single potentially memory leaking operation, every time a write another snippet of code. Not to mention that adding code in one place may cause an unexpected memory leak in another part of the application.
It's just that I wrote an application that had a huge memory leak, and I had to start from scratch. When I develop the application this time around, I want to make sure my unit tests can detect that I've just created a memory leak, and I can eliminate it as soon as possible.
I think I've seen tools like this for C++, but not for Javascript. Does anyone know of any? Thank you!
Is there a way to unit test for javascript memory leaks? What I mean is, is there any way to access the heap directly from javascript code to check for detached DOM trees or increased memory usage?
I know you can do this from Chrome Dev Tools, but I'm wondering if there's a way to do it directly from my unit tests, since it seems tedious to write some code, take a heap snapshot, perform a potentially memory leaking operation, take another heap snapshot, and repeat for every single potentially memory leaking operation, every time a write another snippet of code. Not to mention that adding code in one place may cause an unexpected memory leak in another part of the application.
It's just that I wrote an application that had a huge memory leak, and I had to start from scratch. When I develop the application this time around, I want to make sure my unit tests can detect that I've just created a memory leak, and I can eliminate it as soon as possible.
I think I've seen tools like this for C++, but not for Javascript. Does anyone know of any? Thank you!
Share Improve this question edited Jul 21, 2013 at 23:27 Jacquerie asked Jul 21, 2013 at 23:13 JacquerieJacquerie 3543 silver badges8 bronze badges 1- Chrome developer tools -> Profiles. – elclanrs Commented Jul 21, 2013 at 23:14
3 Answers
Reset to default 5According to MDN's docs on window.performance, Google Chrome has a non-standard extension (window.performance.memory) that gives access to values like usedJSHeapSize, totalJSHeapSize, jsHeapSizeLimit.
To get byte-level precision, you need to use the --enable-precise-memory-info
flag.
For Garbage Collection, the only way I've found to force a browser to perform GC is with Chromium, with a special mand flag. When you run this mand:
chromium-browser --js-flags='--expose_gc'
you get access to the method window.gc()
, which you can call to force GC.
This may open the possibility for testing memory usage in unit tests, for example.
For Node.js there's leakage that can be used to perform memory related unit tests. If your test involves DOM, you may try using jsdom to simulate the browser behavior, but I cannot guarantee it will give you the same result.
To check memory leaks, You need to have access to memory allocation size or size of Your variables. There's no possibility to do it in JavaScript.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745300994a4621432.html
评论列表(0条)