java memory leak

Discussion in 'OT Technology' started by Grifter, Jul 6, 2006.

  1. Grifter

    Grifter Silver Member

    Joined:
    Dec 21, 2000
    Messages:
    3,117
    Likes Received:
    0
    Location:
    the southwest
    Would anyone know of any tool that would help us identify a memory leak in java code?
     
  2. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
  3. GunboatDiplomat

    GunboatDiplomat New Member

    Joined:
    Jun 9, 2006
    Messages:
    214
    Likes Received:
    0
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    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.
     
  5. Doc Brown

    Doc Brown Don't make me make you my hobby

    Joined:
    Mar 31, 2006
    Messages:
    16,404
    Likes Received:
    0
    Location:
    Ohio

    That made my head hurt.

    :bowdown:
     
  6. GunboatDiplomat

    GunboatDiplomat New Member

    Joined:
    Jun 9, 2006
    Messages:
    214
    Likes Received:
    0
    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...
     
  7. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    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.
     

Share This Page