• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Out of memory error:java heap space ?

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the reason behind Out of memory error:java heap space ?

 
Stephan van Hulst
Bartender
Pie
Posts: 5885
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9295
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at our FAQ: InvestigateOutOfMemoryError
 
Bhay Zone
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bravi kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good coding practices don't lead to such errors.Always clean the resource once it's done.
 
Anish Nath
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One line java program that throw "java.lang.OutOfMemoryError: Java heap space Analysis Eclipse MAT
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic