View Single Post
Old 13-01-2018, 02:54 PM   #2
Arch-Supremacy Member
davidktw's Avatar
Join Date: Apr 2010
Posts: 10,032
Can someone explain memory leak? I can't understand the idea (especially in JavaScript context). Read a lot, even eli5 also cannot quite understand

Best if someone can give simplified example + comments

Memory leak simply are memory consumed by the processes and cannot be returned for useful usage within the lifespan of the process.

For example, in C/C++, you can allocate some memory from the heap, and do not assign it to any pointer or referenced by any means. Don’t deallocate it either. This is memory leaked, because without any reference to the heap, you simply cannot use it to store. It is like losing a container in the sea, you cannot be searching the whole sea to find a container. Remember the lost planes that we still can’t find today? Same idea.

In programming languages with automatic memory management like Javascript, Java, PHP, Python, Ruby etc, it will be normally not possible for the developer to create memory leak because memory are reclaimed by the AMM unit. However since these runtime machines of these languages are normally written using lower level languages like C/C++, integrally memory leak can be introduced.

Some cyclic data structures are potential memory leaks because they can exist within the lifespan of the process but cannot be reclaimed back without using a sort of mark and sweep method in memory reclaim action process, which not all garbage collection system are that mature. So these data structures when left alone will keep on take up memory thru out the lifespan of the process.

For javascript, you can read

Last edited by davidktw; 13-01-2018 at 02:56 PM..
davidktw is offline   Reply With Quote