Familiar but Not Understood: Pointer vs Reference
I was watching Tsoding, where he questioned what a memory leak really means. He argues the definition is unclear for a few reasons: “Not freed before exiting” doesn’t help much because it is usually a good practice to pre-allocate a fixed size of memory, use it for the entirety of the program, and exiting doesn’t cause any issue. So we don’t really call this a leak. “Unreachable memory” has limits too. This definition sounds better, but it breaks down in practice. In some languages, you can manipulate pointers directly, which can trick a garbage collector into thinking memory is unreachable when it’s still in use. But even in languages like JavaScript where you don’t have access to pointers, you can “leak” memory by forgetting to clean up callbacks that hold references to objects. Those objects should be unreachable, but they’re not so memory keeps growing. You have a garbage collector and still leak memory. This is why Tsoding says there’s no clear definition of a memory leak. Tsoding suggests that a memory leak is more about what the programmer meant to do, not a strict rule. He also makes a deeper point that memory allocation itself is an artificial idea built on top of a fundamental computational model. So “leaks” are really just problems we created for ourselves. ...