Results 1 to 3 of 3
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default How to use the garbaje collector

    Garbage collections will be handled by Java Virtual Machine itself means ,how does it know that the object is no longer needed by the program.
    Memory recycling, if i use system.gc whether it's a worst programming style or optimized one.
    Thanks.

    Eric

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    As a general statement you don't care if an object is no longer needed. But this means that you need to be careful. Unlike C++ you don't have an explicit destructor. Therefore you need to make sure you clean up after anything like database connections before the object goes out of scope.

    I guess I'm not a fan of System.gc() - before the Java Virtual Machine throws an OutOfMemoryException it will do everything it can to clean up memory. If you're calling System.gc() it may help a little bit but I guess I question that it is going to do much for you. A decent Java program doesn't need to do it - know where memory is allocated and make sure you don't have references to it when youd don't want them.
    greetings,

    Felissa:p

  3. #3
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default

    Overly simple explanation: the Java Virtual Machine will know to collect an object when it is no longer accessible by the code.

    Example:
    Java Code:
    Object o = new Object();
     o = null; // the previously-created object is now unreachable
               // and is eligible for GC
    Greetings.

    Daniel:o

Similar Threads

  1. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 09:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •