The Artima Developer Community
Sponsored Link

Legacy Design Forum
Finalization and Cleanup



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


WeakReference - It works!

Posted by Amrinder Arora on June 28, 2001 at 6:15 PM

I added some lines so that now your code looks like:

a = null;
String []stringArr = new String[1000];
for (int i=0;i<1000;i++)
stringArr[i] = new String ("Stringsssssssssssssssssssss"+i+ "sssssssssss"+
//Call garbage collector...

Now I get the desired output.

If however, you still dont get the desired output, maybe because your system has more memory, you can play with number 1000.


> > I am writing a GUI for Sun's command-line javac, mainly for educational purposes, but also hoping to get use out of it. I've reached the part where I deal with opening and saving files. In order to ensure that no file is ever being written to and read at the same time, I'm creating a class that will sit in memory and keep track of all the files I'm using. That way I'll be able to synchronize on the File objects. My problem is that if I use regular references in the manager class, File will not be garbage collected, nor will file handles be released. I was hoping to make a Vector of WeakReferences to the objects, since that would (theoretically) cause the GC to kill the File off for me when I was no longer using it. Unfortunately, a test program a I wrote seems o be telling me that I'm not using WeakReferences correctly:

> > import java.lang.ref.*;

> > public class test
> > {
> > public static void main(String[] args)
> > {
> > String a = new String("This object is uncollected!");
> > WeakReference b = new WeakReference(a);
> > System.out.println(b.get());
> > //Remove strong reference to the String
> > a = null;
> > //Call garbage collector...
> > System.gc();
> > //Shouldn't the String be finalized?
> > if(b.get() == null)
> > System.out.println("It works!");
> > else // This is the condition that is executed
> > System.out.println(b.get());
> > System.exit(0);
> > }
> > }

> > This program was compiled and run under Sun's JDK 1.2.2 compiler, and the output was:

> > This object is uncollected!
> > This object is uncollected!

> > Why didn't the second line say, "It works!"? Any help or explanation on WeakReferences or the GC would be appreciated.
> >
> Well, one thing that may be happening is that System.gc() is
> not guaranteed to cause the garbage collector to collect
> everything. System.gc() is just a hint to the garbage collector
> that now is a good time to collect, but the garbage collector
> can decide whether or not to do anything.

> Here's what the javadoc for System.gc() says:

> Calling the gc method suggests that the Java Virtual Machine
> expend effort toward recycling unused objects in order to
> make the memory they currently occupy available for quick
> reuse. When control returns from the method call, the Java
> Virtual Machine has made a best effort to reclaim space from
> all discarded objects.

> For an example of a garbage collector that would usually ignore
> the System.gc() suggestion completely, consider a copying
> collector. That kind of garbage collector doesn't collect
> until it fills up half of the heap. So if you heap doesn't
> happen to be half full when you call System.gc(), no objects
> will be reclaimed.

> bv


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us