File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to get class variable ready for GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get class variable ready for GC" Watch "How to get class variable ready for GC" New topic
Author

How to get class variable ready for GC

Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
I have a linked list class (List) which I wrote myself.. I have two variables holding this list in my application class (head, current).

In order to get rid of the memory absorbed by these variables when a new file is opened, I set them each to null and run System.gc(), then begin to populate them again from scratch with a Link constructor.

I know that System.gc() is only a suggestion, not a command, so I ran it 10 times in a loop to help persuade it. However, memory is never freed up.

Here is my code attempting to reclaim memory.

Any suggestions as to getting that memory back would be appreciated.

Here is my Link class.

[ March 24, 2005: Message edited by: Jeff Grant ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Jeff,

Sun's JVMs are written so that memory is never given back to the OS; free memory stays in the Java heap, ready to be reused. If you're checking for memory being reclaimed using your Windows Task Manager (or similar) then you'll never see memory being freed.

Instead, use Runtime.getRuntime().freeMemory() to get the amount of unallocated space in the Java heap. You should see this value go up when you free your list.


[Jess in Action][AskingGoodQuestions]
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
You are correct, I was looking at windows task manager. I saw the Runtime function you suggested and attempted to use it in a different location.. it gave an exception saying it cannot be run in a non-static area, so I figured task manager was the next best thing.

Task Manager did work previously to view memory usage in a prevoius JSP project, but I suppose the "app" is ended after each page is loaded.

Now I see memory being freed up as you suggest.

My application loads a file with 100,000+ lines. Each line gets placed into my list with a non-static number of sub elements. At 100,000 lines I have it top and tell the user it has trunkated the output to 100,000 lines due to memory constraints.

As you can see in my memory output above, it freed up a lot of memory. However, about 20,000 lines into reading the same file for the second time, it stalls and proceeds very slowly.

So as it seems to have freed up the memory, it is not going anywhere near as fast the second time around.. so slow in fact that from 20,000 to 25,000 lines, it took 4 minutes where as the original 100,000 line read only took 3 minutes 30 seconds.

Suggestions on this new problem?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Are you in JDK 5? There is a JConsole tool in the JDK that let's you watch memory and thread use in real time. Very cool.

You may find that no matter how many times you call gc() the JVM thinks it doesn't really need to GC right now and ignores you, then when it truly runs out of heap space and does a GC it chews up some time and resources. I think there's not much you can do about that.

BTW: Are you sure there are no other references to any objects in your linked list? If you have a stray reference to the anchor of a list the members will never be collected.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
I'm running Java 1.4.2
Originally posted by Stan James:
BTW: Are you sure there are no other references to any objects in your linked list? If you have a stray reference to the anchor of a list the members will never be collected.

I thought of that.. all of my globals are cleared out to null.
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Since we can see that a considerable amount of memory is freed up by my memory displays above, are we still assuming that something is referencing my link list now allowing GC to clear it out?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup, I missed the fact that you are getting a big chunk back. Watch over many reps and see if you always get it all back. I wouldn't expect a memory leak to cause your slowdown unless it's really bad.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get class variable ready for GC
 
Similar Threads
Liked List help
garbage collection and finalize.
link list
LinkedList
node class for linked list