Deallocating storage

M

milkyway

Hello,

I have an array of objects that were created something like

Array[1] = new object(object_name);
Array[2] = new object(object_name);

How would one deallocate the storage that was created with the "new"
statement in this situation? Can I just do something like:

Array[1] = null;
Array[2] = null;

and hope that there is some automatic /magic garbage collection process
working?

Any help is appreciated ;-P

TIA
 
R

richard.anderson970

milkyway said:
Hello,

I have an array of objects that were created something like

Array[1] = new object(object_name);
Array[2] = new object(object_name);

How would one deallocate the storage that was created with the "new"
statement in this situation? Can I just do something like:

Array[1] = null;
Array[2] = null;

and hope that there is some automatic /magic garbage collection process
working?

Any help is appreciated ;-P

TIA

See http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html
 
K

klynn47

When all references to an object are lost, it becomes garbage and is
eligible for garbage collection. But there is no guarantee that it will
be collected.
 
J

John C. Bollinger

When all references to an object are lost, it becomes garbage and is
eligible for garbage collection.
Yes.

But there is no guarantee that it will
be collected.

Pretty much true. Java guarantees, however, that no OutOfMemoryError
will be thrown before the VM makes its best effort to free enough memory
to accommodate the program's needs. In other words, GC will definitely
happen if it is needed, though one cannot say whether or not a
_particular_ eligible object will be collected, and if so, how soon
after it becomes eligible. When the VM shuts down, it releases all its
resources, of course, which is why it may be that no actual GC cycle
needs to happen during the execution of some programs.


John Bollinger
(e-mail address removed)
 
M

milkyway

Then what is the best way to get rid of something that has been
allocated? In my example above, I am setting an item in an array to
null. Is there a better way to accomplish the task?
 
S

Steve Horsley

milkyway said:
Then what is the best way to get rid of something that has been
allocated? In my example above, I am setting an item in an array to
null. Is there a better way to accomplish the task?
Even this is only necessary if you are going to retain a reference
to the array for some time. Setting the array reference to null
also makes the contents unreachable of course (if there are no
other references).

or re-pointing it to a different object,
or simply letting the array reference go out of scope by returning
from your called method will serve just as well.

But if you want to drop a single object, then setting the reference
to null, setting it to point to a different object, or simply letting
the reference go out of scope by returning from your called method
is indeed the way to do it.

Steve
 
J

John C. Bollinger

milkyway said:
Then what is the best way to get rid of something that has been
allocated? In my example above, I am setting an item in an array to
null. Is there a better way to accomplish the task?

The Garbage Collector is a core Java feature. It takes care of the
actual "getting rid of" objects. You generally do not need to know the
details, and you should not be concerned about trying to force it to
happen. You do need to make sure that you do not hold on to references
to unneeded objects, but you don't necessarily need to explicitly set
variables or array slots to null to accomplish that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top