how to free java internal class instances

Hi,

My graphics application is showing heapOutOfMemory error if it is run continuosly for some hours.

I used optimizeit tool in jbuilder to track objects which are not freed (garbage collected).

It revealed some java internal objects and myown classes instance which are not freed.

I freed all objects which i have created(class instances).

How can i free those java internal classes, can anyone help?

for your info, i will list down some of these classes in descending order of heap they consume:

int[]

Char[]

com.sun.org.apache.xerces.internal.dom.Elementlmpl

java.lang.String

com.sun.org.apache.xerces.internal.dom.TextImpl etc.

incase of in int[] ,

initialy (just main window): consumes arounnd 600KB of heap

1.after opening sub window: consumes 1200K of heap

2.after closing the sub window: comes to around 900KB

if repeat the steps 1 and 2 the heap size will keep on increasing gradualy.

can some one explain why it is happening? and also a solution for this?

Thanks in advance.

Tom.

[1113 byte] By [tom_josea] at [2007-11-27 5:14:51]
# 1
Make sure that you are not declaring new variables in for loop other loops.Assign null to variables once you are done.Read more on good coding practices in java (google.com)
polimetlaa at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 2
Some classes offer methods with names such as dispose() or close().Make sure you are calling those methods as appropriate.
baftosa at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 3
> Make sure that you are not declaring new variables in> for loop other loops.I do not think that this is a valid issue - could you elaborate with an example of this causing a problem?
jbisha at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 4
Possibly set your sub window to null once you close it? Maybe that will trigger the JVM to release the mem.
cell@techa at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 5

Run this example and check the log.

import java.util.ArrayList;

public class Memory {

public static void main(String s[]) {

System.out.println("correctDeclaration");

correctDeclaration();

System.out.println("wrongDeclaration");

wrongDeclaration();

}

public static void wrongDeclaration() {

long heapSize = Runtime.getRuntime().totalMemory();

System.out.println("total memory-->" + heapSize);

try {

for (int i = 0; i < 1000; i = i + 10) {

ArrayList al = new ArrayList();

String s2[] = new String;

long heapFreeSize = Runtime.getRuntime().freeMemory();

System.out.println("I value-->" + i + " Memory left-->"

+ heapFreeSize);

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

public static void correctDeclaration() {

long heapSize = Runtime.getRuntime().totalMemory();

System.out.println("total memory-->" + heapSize);

try {

ArrayList al = new ArrayList();

String s2[] = new String[10];

for (int i = 0; i < 1000; i = i + 10) {

long heapFreeSize = Runtime.getRuntime().freeMemory();

System.out.println("I value-->" + i + " Memory left-->"

+ heapFreeSize);

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

Also see http://forum.java.sun.com/thread.jspa?threadID=439431&messageID=1978093

Regards,

Bhavani

polimetlaa at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 6

> Run this example and check the log.

>

. . .

> Also see

> http://forum.java.sun.com/thread.jspa?threadID=439431&

> messageID=1978093

>

> Regards,

> Bhavani

I am not sure as to the relevance of the link.

I do not believe that your example illustrates your original point ?the declaring of the reference in the loop has nothing to do with the memory usage in your program. The fact that you are creating objects in the loop is the reason for the memory growth.

Although, creating unnecessary objects is not a good practice, it is not what your original post was referring.

Also, your example program will not cause an OutOfMemoryException because the objects created are eligible for garbage collection at the end of each loop cycle and the JVM will always free space if possible before throwing the exception. To see this, change the first loop count to some huge number and you will see that the memory usage stops going up at some point.

jbisha at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 7

Hi JBish,

>Make sure that you are not declaring new variables in

> for loop other loops.

I am taking this statement back?I want to say that don抰 declare and create objects, if it is not required.

Even though it is eligible for garbage collection, we don抰 have any control on this. It may cause Memory Out of Exception based on situation.

Hi tom jose,

Please paste code causing heapOutOfMemory error.

So that we can try to give exact solution.

Regards,

Bhavani.

polimetlaa at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 8

> Hi JBish,

>

> >Make sure that you are not declaring new variables

> in

> > for loop other loops.

>

> I am taking this statement back?I want to say that

> don抰 declare and create objects, if it is not

> required.

OK

> Even though it is eligible for garbage collection, we

> don抰 have any control on this. It may cause Memory

> Out of Exception based on situation.

No, this is incorrect. If objects are available for GC then the JVM will collect them before throwing the OutOfMemoryException. It may be that there still is not enough memory after they are collected but it will not throw the exception until there is nothing left to collect.

jbisha at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...
# 9
Please give me one simple example which will demonstrate heapOutOfMemory error.If possible please give me pointer to JVM specification where it talks clearly about GC work as you described. http://java.sun.com/docs/hotspot/gc1.4.2/Thank you,Bhavani
polimetlaa at 2007-7-12 10:36:55 > top of Java-index,Java Essentials,Java Programming...