Results 1 to 3 of 3

Thread: garbage collection *resolved*

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

    garbage collection *resolved*

    could somebody explain how java uses garbage collection to reclaim unused memory space?

    From reading that I have done, I am not sure which way it goes about it:

    my example:

    TownBuilding = new Building("Town Hall"); //(a)
    TownBuilding = new Building("Public Library"); //(b)

    When b is coded, does java simply overwrite the contents or does it create a new Variable (b) and leave (a) for garbage collection?

    Very confused...
    Last edited by mbonfyre; Aug 18th, 2003 at 05:31 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not sure what you mean with your question, so here are two answers.

    1 (simple)
    Non-primitive variables in Java aren't the objects, they are only references. When you do a new, the VM creates an actual object on the heap and returns a reference to it, which you can then assign to a reference you declared and use the object through it.
    When you assign a new value to the reference, the old object lies on the heap waiting for GC.

    2 (advanced)
    There is no object reuse unless you explicitly implement it for a class (via a static factory method). An exception might be java.lang.String, depending on the JVM implementation.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yes CornedBee is right. Also, scope dictates when an object or objects are eligible for garbage collection. For the two objects you have created there would be no overwriting of data. Both objects would reside in memory as two seperate objects with any static member variables being shared by both.

    TownBuilding t1 = new Building("Town Hall"); //(a)
    TownBuilding t2 = new Building("Public Library"); //(b)

Posting Permissions

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



Click Here to Expand Forum to Full Width