www.hardwarezone.com.sg

www.hardwarezone.com.sg (/)
-   The Programmers Den (https://forums.hardwarezone.com.sg/programmers-den-296/)
-   -   Explain memory leak + Example (Help) (https://forums.hardwarezone.com.sg/programmers-den-296/explain-memory-leak-example-help-5757737.html)

weathersohot 13-01-2018 12:27 PM

Explain memory leak + Example (Help)
 
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 13-01-2018 02:54 PM

Quote:

Originally Posted by weathersohot (Post 112291885)
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...t-rid-of-them/


All times are GMT +8. The time now is 09:36 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Copyright © SPH Magazines Pte Ltd. All rights reserved.