|
-
Aug 17th, 2003, 11:56 PM
#1
Thread Starter
Addicted Member
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.
-
Aug 18th, 2003, 05:09 AM
#2
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.
-
Aug 18th, 2003, 11:35 PM
#3
Dazed Member
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)
Last edited by Dilenger4; Aug 18th, 2003 at 11:39 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|