aspose file tools*
The moose likes Performance and the fly likes Out of memory error:java heap space ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Out of memory error:java heap space ?" Watch "Out of memory error:java heap space ?" New topic
Author

Out of memory error:java heap space ?

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
what is the reason behind Out of memory error:java heap space ?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

You are probably allocating a lot of objects in a long (possibly infinite) loop, or you have a memory leak somewhere (e.g. storing object references in a list and forgetting to null them out when you're done with them).
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

Have a look at our FAQ: InvestigateOutOfMemoryError


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Bhay Zone
Greenhorn

Joined: Jul 13, 2009
Posts: 7
The error is thrown when the garbage collector is unable to free up enough space to allow allocation to the requested object.

So basically when a new object is to be created and there is not enough space in the heap, the jvm will invoke the garbage collector. If after the gc completion, there is still not enough space freed up, then the jvm throws an OutOfMemoryError: java heap.

Note that even though your post mentions "java heap space", there are a few other types of OutOfMemoryErrors that the JVM can throw.

1) OutOfMemoryError - when not enough memory can be allocated for a new array
2) OutOfMemoryError - when not enough memory is available for your class to load
3) OutOfMemoryError - when too many threads / not enough stack space available for a new thread
4) StackOverFlowError - when you have exceed the maximum space on the stack.
Bravi kumar
Greenhorn

Joined: May 03, 2012
Posts: 3
Stephan van Hulst wrote:You are probably allocating a lot of objects in a long (possibly infinite) loop, or you have a memory leak somewhere (e.g. storing object references in a list and forgetting to null them out when you're done with them).


Hi,

i am also getting outofmemoryerror :java heap space

can you please help me out in fixing this error.

code:

public static Vector multiplyVectors(Vector vect1,Vector vect2){

Vector resultVector=new Vector();
for(int i=0;i<vect1.size();i++){
for(int j=0;j<vect2.size();j++){

vector v=new Vector();

v.addAll((Vector)vect1.elementAt(i));
v.addAll((Vector)vect2.elementAt(j));

resultVect.add(v);

}
}
}

in the above program, both vector sizes reaches to more than 5000 causes outofmemoryerror.


Thanks for help in advance.>
Raghav Viswanathan
Greenhorn

Joined: Apr 26, 2012
Posts: 26

Hello Bravi Kumar,

Welcome to the Ranch. . Happy Forum-ing



Are you sure you want to create a new vector object for every time loop runs. The code actually creates a lot of vector objects in heap ( For example Vect1.size * vect2.size(), let us assume that each of the vector's were 100 in size, then you would end up creating 10,000 vector objects in the heap.) I am not sure if your intention was exactly this.

furthermore,

v.addAll((Vector)vect1.elementAt(i)); // here you are creating/ Casting each of the objects in the vector into a vector and assigning it into another vector and you are creating/ overriding the values when the loop runs for a second time.

Check your code once again or let us know the objective of the code/class or method , so that we can help you with the logic.

Thanks and regards
Raghav.V


Better late than never.
Bravi kumar
Greenhorn

Joined: May 03, 2012
Posts: 3
Hello Raghava,

Yes i am sure about creating new vector object for every loop runs.

Difference between creating objects inside for loops and outside for loops

Inside for loop

for(...;..;..){
Vector v=new Vector();
v.add(any object);
}

In the above code for every loop runs a new Vector object is created so that we can't see previously created object as it was already cleaned by garbage collector.

Outside for loop.

Vector v=new Vector();
for(...;..;..){
v.add(any object);
}

In the above code, we are creating only one vector object and adding millons of objects to it causes to some what error like outofmemoryerror.. Here we are not allowing GC to clean objects which are no longer used.

Creating objects inside or outside depends on the situation.

furthermore whatever i coded earlier might be correct. i can't explain the intention as it was developed by some other developer and i am maintaining the project.


Thanks & Regards,
Ravi Kumar. B


Rajit vreddi
Greenhorn

Joined: Jul 05, 2012
Posts: 17
good coding practices don't lead to such errors.Always clean the resource once it's done.
Anish Nath
Greenhorn

Joined: Sep 21, 2011
Posts: 23
One line java program that throw "java.lang.OutOfMemoryError: Java heap space Analysis Eclipse MAT
 
Consider Paul's rocket mass heater.
 
subject: Out of memory error:java heap space ?