Discussion in 'OT Technology' started by Grifter, Jul 6, 2006.
Would anyone know of any tool that would help us identify a memory leak in java code?
I use JProfiler all the time at work. Easiest profiler I've ever used.
Am I missing something, here? Doesn't Java use garbage collection?
For Java's automated garbage collection to work, the programmer has to remember to delete all references to a given memory space. If references persist, Java won't free the memory, even if the code isn't actually using the memory for anything useful. Remember, it's impossible for the compiler (or interpreter, in this case) to know what purpose the code serves.
Check all of your pointers and iterators, and make sure that every single one gets nullified when it isn't needed anymore. To ensure this, the process I use when writing code is to immediately nullify every pointer I declare, and then insert the rest of my code in between the pointer declaration and the nullifier. Similarly, whenever I open an IF block, I immediately insert an END IF, and then I put my conditional code inside the block.
Writing code in a linear manner (instead of a hierarchical manner) is a bad idea all around, because it's easier to forget to finish what you start.
That made my head hurt.
Correct me if I'm wrong (since I know nothing about Java) but if a reference goes out of scope, isn't the object the reference referenced dereferenced? (say that three times fast!) If this is so then it should be very hard to "leak" memory (the memory isn't really leaked 'cause you can get it back). That's why garbage collection was considered such a God-send. I thought Java's garbage collector was robust, too, so that even circular references are cleaned up...
This is correct, but it only works when objects are dereferenced... Unfortunatly, not everyone writes good code. In fact, most code is bad code. Not utilizing OOP, for example, can start problems. As long as the objects initialized don't go out of scope, then Java will not see them as dereferenced.