Javascript unit testing for memory leak

Is there a unit test way for memory leaks in JavaScript? I mean, is there a way to access the heap directly from javascript code to check individual DOM trees or increase memory usage?

I know that you can do this in Chrome Dev Tools, but I’m wondering if there is a way to do this directly from my unit tests, since it seems tedious to write some code, take a picture of the heap, perform potential memory of the ongoing operation, do another heap snapshot and repeat each operation of a potential memory leak every time you write another piece of code. Not to mention the fact that adding code in one place can cause an unexpected memory leak in another part of the application.

I just wrote an application with a huge memory leak, and I had to start from scratch. When I create the application this time, I want to make sure that my unit tests can detect that I just created a memory leak and that I can fix it as soon as possible.

I think I saw such tools for C ++, but not for Javascript. Somebody knows? Thanks!

+6
source share
2 answers

To check for memory leaks, you need to have access to the size or size of your variables. There is no way to do this in JavaScript.

-2
source

According to the MDN 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 accuracy, you need to use the --enable-precise-memory-info flag.

For Garbage Collection, the only way to get the browser to run GC is through Chromium with a special command flag. When you run this command:

 chromium-browser --js-flags='--expose_gc' 

you get access to the window.gc() method, which you can call to force GC.

This may open, for example, the ability to test memory usage in unit tests.

+1
source

Source: https://habr.com/ru/post/949951/


All Articles