Explain memory leak + Example (Help)

weathersohot

Supremacy Member
Joined
Jun 18, 2012
Messages
7,756
Reaction score
404
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

Thanks :o
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
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

Thanks :o

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 https://auth0.com/blog/four-types-of-leaks-in-your-javascript-code-and-how-to-get-rid-of-them/
 
Last edited:
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top