Avoiding Memory Leask In Your Javascript

memory leaks in java

Memory leaks are dangerous to a program because they eat up the system's memory as the program runs. Some leaks are very minor and can almost be ignored, but more severe leaks will crash your computer in no time at all. Although you will probably end up with a few leaks here or there, understanding common causes of leaks in Javascript should help you improve your code, and reduce the occurrence of leaks.

The Main Cause of Memory Leaks

One of the main issues that Javascript programmers face is two different methods for garbage collection. The Javascript method of garbage collection will take care of two objects pointing at each other as long as there are no other outside objects pointing at either of them. When you are dealing with a reference counting garbage collection system like the one used with Internet Explorer, the two objects can't be erased because they are pointing to each other.

The Danger of Circular References

Circular references are one of the leading causes of memory leaks in Javascript for the reason that I wrote about above. A circular reference is when two different objects reference each other. When that happens they simply circulate back and forth and get stuck causing a memory leak that can build up in some cases.

Circular References in Functions

A less noticeable type of circular reference happens with nested functions in Javascript. Javascript has a nice feature known as closure that allows a nested function access to variables inside the function it is residing in even after that outside function has returned if you link that nested function outside of itself after the main function has returned. Although closure if very helpful, it also causes leaks.

A circular reference is created in this instance by the nested program linking back into the outer program that links back into the nested program again. This circular reference also makes it impossible for garbage collection to occur on the function and you are left with a memory wasting problem.

Fixing the Leaks

The first type of memory leak is pretty simple to fix. After you have utilized the objects that are referencing each simply set the object equal to null which will break the reference and garbage collection can occur as usual.

With the closure leak, you have to create another instance of closure after the first instance that doesn't have a memory leak. This second instance of closure will allow garbage collection to occur on the function as usual.

Locating Leaks

The hardest part of taking care of most memory leaks is actually figuring out where they occur in the first place. sIEVE, a leak detection tool, can make your job a little easier. The tool simply lists all of the DOM objects in the code and tells you how many references each of the objects has. While running the program run certain features of your website and do an additional scan of the DOM objects in your code. You should be able to spot abnormal patterns in your code and eventually target the area where the code goes bad.