• 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

Out of memory error:java heap space ?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the reason behind Out of memory error:java heap space ?

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at our FAQ: InvestigateOutOfMemoryError
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.>
 
Greenhorn
Posts: 26
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good coding practices don't lead to such errors.Always clean the resource once it's done.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One line java program that throw "java.lang.OutOfMemoryError: Java heap space Analysis Eclipse MAT
reply
    Bookmark Topic Watch Topic
  • New Topic