5 ms·
Easy to overthink this: create a memory leak by retaining un-needed references. I forget where I read it, but I once saw GC characterized as changing memory ma
by mschaef 10y ago
Easy to overthink this: create a memory leak by retaining un-needed references.
I forget where I read it, but I once saw GC characterized as changing memory management from managing the nodes of graph to managing the edges. It's more automatic and safer, but there is still a finite heap and there are still pitfalls.
- rbanffy 10y agoJust create a Vector and keep adding things to it, indefinitely. I once encountered that in a production environment (circa 2000). The server kept stats while it was running, appending them to a vector on every iteration of its main loop. On the development environment, on x86 processors, it took days to exhaust the memory and the issue was never spotted. On the production environment, on an 8-way ridiculously fast SPARC machine, it took a couple hours to completely lock up the machine.
- fleg 10y agoThis would also make a leak in languages without a GC, possibly making it quite hard to spot. With traditional leaks, where you're allocating memory and not freeing it, it's quite easy to find the problem using a sanitizer or valgrind. But with a memory wasted like this, which would get nicely cleaned when the program is exiting - well, you need to start a profiler and find the leak by yourself. Can be annoying.
- deleted 10y ago[deleted]
- sorokod 10y agoVectors haven't been used that much this century. What you describe is not a leak. A leak would be when you loose all references to your Vector and memory is not reclaimed by the GC.
- joncrocks 10y agoIt's important to note that in Java you don't have leaks in the same way that you do in C. When someone refers to Java 'leaks', they actually mean "Objects that are kept in memory by references that are never used again, but in theory could be." It's like your code is a hoarder, keeping all those old back-issues of newspapers 'In case I need them again' when in fact they are just taking up space.
- coding123 10y agoYes, it's not a leak but many Java devs are aware of these things and instead of typing out "somewhere in my graph I've retained a reference to something that shouldn't" has been colloquially shortened to "GC Leak" and if you see those two words together, they know what it really means.
- the_af 10y agoI'd say even in Java, infinitely adding elements to a collection is not a memory leak, but merely forgetting that memory is finite. No Java programmer I know would call it a memory leak. A leak is when you think you no longer hold references to an object, but you still do, and therefore the GC cannot free its memory. An example is the accepted answer in the StackOverflow link.
- rbanffy 10y agoThe answer in SO is just a more elaborate instance of this same thing: allocating stuff and forgetting to delete all references to it.
- daemin 10y agoI'd argue that a memory leak is when you've allocated memory that you are not using any more but it has not been freed for some reason. If this happens because the reference has been truly lost, or if there's something else (like a helpful cache) that is keeping it alive, it doesn't matter. It is still leaked and will only be cleaned up when the program exits. This applies to resources in general, if they are only reclaimed by the OS when the program exits, then it is a leak.