2

For a project for school I have to program different kind of algorithms. The problem is, I got a working algorithm. But I have to run it several times and after some time it gives me the following errors:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I know what the error means, but is it possible to let Java search for empty space during the run? I know it uses a lot of space which isn't used at some point. It sets a lot of object to null during the application run and create a lot of new ones, because of this it runs out of memory.

So concrete: is it possible to let the JVM free some space that is set to null? Or free some space in the time the program is running? I know I can set the JVM to more space, but sooner or later I will run to the same problem.

If you need my IDE (in case it is IDE specific) it is Eclipse.

Nadir Muzaffar
  • 4,772
  • 2
  • 32
  • 48
Nando
  • 358
  • 3
  • 13
  • possible duplicate of [How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size)](http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap) – Mike Samuel Jan 17 '12 at 23:12
  • 2
    unless you have very little memory, there's a good chance you're doing something wrong...really shouldnt be facing memory issues for most school projects – Nadir Muzaffar Jan 17 '12 at 23:12
  • http://stackoverflow.com/a/5121287/20394 in particular talks about how to increase the amount of memory when running under Eclipse, and the rest of that article deals with strategies for diagnosing and fixing memory problems. – Mike Samuel Jan 17 '12 at 23:13
  • First step: Get a good understanding of how much memory your program is allowed to use, how much memory your program is supposed to use, and how much it is actually using. – David Schwartz Jan 17 '12 at 23:14
  • It would also be of benefit to share how much system memory you have, in addition to a [rough] estimate of how many objects your program creates. Alternatively, you could trace how much memory is being used by `jconsole` and see how much is being used in what time. – Makoto Jan 17 '12 at 23:20
  • I agree with @Nadir If I was a betting man, I'd bet that you have an issue with an infinite loop. Look at your loops, make sure there is always a way to break out of it. – Bill Jan 17 '12 at 23:23
  • I don't have an infinite loop, but it is a large (really large) loop with a recursion in it. I know that it's not the best solution to code it, but it works. And if I would find a way to make more space I could enlarge my loop instead of coding a lot over again. I have control over the loop length. But the bigger the loop the more accurate the result (think of a bruteforcing algorithm using recursion). I think I will just increase the memory, since I can't manage the garbage collection to throw away my objects (which I set to null). – Nando Jan 17 '12 at 23:58

5 Answers5

2

Please google 'garbage collection'. Java is always looking to reuse space from objects that you aren't using. If you run out of memory, you either need to use -Xmx to configure for more memory, or you have to fix your code to retain fewer objects. You may find that a profiler like jvisualvm would help you find wasteful memory usage.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
1

If you're using an Oracle/Sun JVM, I'd recommend that you download Visual VM 1.3.3, install all the plugins, and start it up. It'll show you what's happening in every heap generation, threads, CPU, objects, etc. It can tell you which class is taking up the most heap space.

You'll figure it out quickly if you have data.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

I would use a memory profiler to determine where the memory is being used. Setting to null rarely helps. The GC will always run and free as much space as possible before you get an OOME.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Q: "is it possible to let the JVM free some space that is set to null? Or free some space in the time the program is running?"

A: Yes, use a call to System.gc() will do this, but this will not likely solve your problem as the system does this automatically from time to time. You need to find the object that is using all the memory and fix it in your code. Likely a list that is never cleared and only ever added to.

I actually encountered this issue while implementing a particularly complicated algorithm that required a massive data structure. I had to come and post a question on this website. It turned out I had to use a completely different type of object altogether in order to avoid the memory error.

Here is that question.

Community
  • 1
  • 1
gnomed
  • 5,483
  • 2
  • 26
  • 28
  • Thanks, that would be a very useful answer. I can use a pause in the loop let Java collect it's carbage and check if it works again. – Nando Jan 18 '12 at 00:01
0

GC will reclaim 'unused' memory automatically, so yes, it is possible to free some space at runtime, but it's crucial to understand what's classified as possible to be reclaimed. Basically an object's space can be reclaimed (garbage collected) if the object itself is unreachable - there are no references to it. When you say 'setting space to null' you're most likely removing just one link (reference) to the object by setting it to null. This will allow to reclaim the object only if that was the only link (reference)

Object First= new Object();   //first object
Object Second= new Object();  //second object 
Object SecondPrim=Second;     //second reference to second object

First=null;   
// First memory will be reclaimed (sooner or later)
Second=null;
// there is still a reference to second object via SecondPrim
// second object will not be reclaimed

Hope this helps. As for checking what's exactly going on I would second advice to profile your program.

wmz
  • 3,645
  • 1
  • 14
  • 22