Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Best Memory Leak Definition [closed]

I feel like developers talk about memory leaks but when you ask them what that means many have no idea. To prevent these situations, let's decide on one.

Please no Wikipedia definitions...

What is your best definition of a memory leak and what is the best way to prevent them?

like image 889
ewakened Avatar asked Nov 23 '08 03:11

ewakened


People also ask

What is the meaning of memory leak?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

Does Closure cause memory leak?

In simple terms, a closure is an inner function that has access to the outer function's scope. In the example above, largeArray is never returned and cannot be reached by garbage collector, significantly increasing its size through repeated calls of inner functions, resulting in a memory leak.

Why memory leak is called leak?

Memory was shared between any running jobs and was called a "pool" of memory after things like motor pools, secretarial pools, and similar. When a job (program) caused memory to become inaccessible, that memory "leaked" out of the pool.


2 Answers

There are two definitions (at least for me):

Naive definition: Failure to release unreachable memory, which can no longer be allocated again by any process during execution of the allocating process. This can mostly be cured by using GC (Garbage Collection) techniques or detected by automated tools.

Subtle definition: Failure to release reachable memory which is no longer needed for your program to function correctly. This is nearly impossible to detect with automated tools or by programmers who are not familiar with the code. While technically it is not a leak, it has the same implications as the naive one. This is not my own idea only. You can come across projects that are written in a garbage collected language but still mention fixing memory leaks in their changelogs.

like image 186
artificialidiot Avatar answered Sep 27 '22 20:09

artificialidiot


Allocated memory that cannot be used because the reference to it has been lost.

like image 42
mthurlin Avatar answered Sep 27 '22 19:09

mthurlin