• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get class variable ready for GC

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic