The Artima Developer Community
Sponsored Link

Design Forum
Help Required-- Object Cleanup

2 replies on 1 page. Most recent reply: Apr 18, 2002 4:47 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
kalai

Posts: 1
Nickname: kamp007
Registered: Apr, 2002

Help Required-- Object Cleanup Posted: Apr 16, 2002 4:54 AM
Reply to this message Reply
Advertisement
Hi all,

I am having big trouble with memory management. I basically have an object which runs 10 threads that reads thousands of files. My problem is, as the program runs it keeps on getting slower. I understand that by letting go of all the references to particular object, gc will retrive the memory. But can u explain how to do this(deleting references) with simple example(s).

And one more thing, when i run this program, in the task manager, the CPU usage is 98%, while memory usage remains constant and there is a lot of free memory still available. Does that mean that memory usage is not the real bottleneck of this execution? How can i find out and reduce cause of such high CPU usage?

Thanks in adavance,
Kalai :)


chohan

Posts: 2
Nickname: chohan
Registered: Apr, 2002

Re: Help Required-- Object Cleanup Posted: Apr 18, 2002 11:19 AM
Reply to this message Reply
Here is what I would do:
- Make as many member variables static as possible to avoid their creation if possible.
- Create objects in innermost scope. Like try defining objects at the place they are being used and then make reference null like
{
someObject = new SomeObject();
// Use it here
someObject = null;
}

- Use Xms - Xms flags for JRE to set the heap size to 50% of the total memory on your machine. Set both ms and mx to same value.

- Make sure that no object is being created in the loop.

Hope it helps

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Help Required-- Object Cleanup Posted: Apr 18, 2002 4:47 PM
Reply to this message Reply
To bring one step further what chohan wrote ...
I usually have all my classes implementing the Destructable interface (even my JUnit tests :-) )
public interface Destructable
{
  public void destruct ();
}

Then in evey single class as advised, you can also nullify all the references :
public class MyCustomClass
  implements Destructable
{
  // Do you own business & then ...
  // add this
 
  public void destruct ()
  {
     ObjRef = null;
  }
}


I always advise not to call System.gc; unless you can do so always after nullifying many reference. Do do it after many objects have been cut appart.

I usually try to have the finalize () interacting intelligently with the destruct interface but it can be risky ! Say you have destruct an Object & then nullified it ... If you call the method a second time the call to destruct () to the object you have references to will generate an NullPointerException that you should trap.

Now another comment about your Threads.
May be you should make sure that they clearly step out of the run () method. As a matter of fact, if the run method does not run out the Threads stay active & the Class cannot be GCed - as well as all the classes (instances of ...) that are referenced by it.

Also if you have many Frames & JFrames, you should make sure you invoke the dispose () method each time !

Also you should make sure you always do a late initialization of all the Object an instance references.

Say you have a class holding a reference to a HashMap. Here is a bit of code you will find in many occasion :
public class MyCustomClass
{
  // This instance is almost immediately marked for GC
  // (as soon as teh constructor is run)
  public HashMap hm = (HashMap) new HashTable();
 
  MyCustomClass ()
  {
     hm = (HashMap) new HashTable();
  }
 
  public void setValue (...)
  {
    hm.setValue (...); 
  }
 
  HashMap getMap()
  {
    return hm;
  }
}


With late initialization, you could then do like this :
public class MyCustomClass
{
  // This instance is almost immediately marked for GC
  // (as soon as teh constructor is run)
  public HashMap hm = null;
 
  MyCustomClass ()
  {
  }
 
  public void setValue (...)
  {
    if (hm == null)
      hm = (HashMap) new HashTable();
    hm.setValue (...); 
  }
 
  HashMap getMap()
  {
    if (hm == null)
      hm = (HashMap) new HashTable();
    return hm;
  }
 
  // Provide a proper destruct ()
  // First removing hm content & then emptying it !
}


Finally,
For performance issues O Reilly has a very good book about it. Use also JunitPerf (see on www.junit.org for references).

Rgds,

Thomas,



At IBM there is a tutorial on how to do that properly.
I remember pposting it to a person I "worship" :-))
Chuck a look there !
http://www-105.ibm.com/developerworks/education.nsf/dw/java-onlinecourse-bynewest?OpenDocument&Count=500

Flat View: This topic has 2 replies on 1 page
Topic: Help Required-- Object Cleanup Previous Topic   Next Topic Topic: Design help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use