Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to get rid of memory leaks in javascript?


Asked by Rhea Knox on Dec 07, 2021 JavaScript



Find out if memory is periodically increasing. The timeline view is great for this. Open the example in Chrome, open the Dev Tools, go to timeline, select memory and click the record button. Then go to the page and click The Button to start leaking memory.
One may also ask,
To avoid memory leak, the best practice is to put var example inside listener, which makes it a local variable. When var example is deleted the path for the object will cutoff,allowing the garbage collector to release memory.
Moreover, JavaScript has automatic memory management. Memory containing objects which are no longer referenced will be eligible for garbage collection, unless you have a memory leak. There is generally no need to manually assign undefined to variables.
Subsequently,
The above example is likely to cause a memory leak because the variable requests, which holds a new instance of the Map object, is global. Thus, every time a request hits the server, there is a memory allocation to the object. The guaranteed memory allocation to the new instance means that the object will live forever.
Likewise,
There are two types of leaks in this case: daemon threads and static fields. Static fields are started with the classloader’s value. Even as Jetty stops deploying and then redeploys your web application, the static reference persists, so the object cannot be cleared from memory.