Link to home
Start Free TrialLog in
Avatar of ap9
ap9

asked on

What causes OutOfMemoryError?


Apparently running out of memory, obviously, given the error message.  :)

But I'd like to know why, as it appears to be inconsistent.  I've written two Java programs (a traffic simulator and a lava lamp animation) and both run fine on the SunSPARC (that's where I originally wrote and tested it) -- I can keep it running for hours without a problem.  But when I brought the source (not the bytecode) back to my PC, compiled, and ran it, it gives that Java.OutOfMemoryError within 30 seconds or so (for both applications).  My machine has more physical RAM and HD space than the SPARC (9GB of disk space [2.3GB free] and 128MB of RAM -- running Win95 OSR2 w/ Symantec Cafe v 1.80).  The SPARC only had 40MB of RAM, and less than 300MB of disk space free.

If I got this error on both the SPARC and my PC, my first guess would be that something is wrong with my code, but since it only seems to happen on my PC, I'm wondering if there is a known bug in the Symantec Cafe JVM or even in Java itself -- but I have not read about this from anyone else, and this would appear to be a rather big issue if it were a problem with Java itself, or even Cafe specifically.  

Any ideas?  Thanks!


AP9
Avatar of sprinkmeier
sprinkmeier

OM does not necessarily mean you machine is out of menory, only that your virtual machine has used up all th ememory it is going to ask for.

try

java -mx100m MemoryHog

to give it up to 100M bytes of RAM. Does that solve it?

Yes, the VM is usually restricted to 60M of memory. Try specifying more.
Avatar of ap9

ASKER

Actually, I went back in and manually added calls to java.lang.System.gc() and it seemed to work for one application, but causes the other to crash when you try to close it.

I'll give your suggestions a try and see if it turns out better.  Thanks!


ap9
Avatar of ap9

ASKER

I've tried increasing max heap size up to 200m, but it still seems to run out of memory in the same amount of time.  I tried increasing the Java and C stack sizes, too, to no effect.


ap9
Desperation time....
- run it with "-verbosegc" on both machines, see if it does a lot more GC on one that the other.
- Try printing out Runting.getMemory() at various points (or some call with a name vaguely like that) to try to see when/where the memory is being used.
- Add a finalizer to your biggest objects and get them to print out the fact that they have been GC'ed. Make sure you get them collected on both systems.
- print out the stack trace of the OM exeption
- purchase and wave vigorously one rubber chicken

I found there was trouble with double buffering, at one time. I have an application that we run under Windows and Linux. One or the other would run out of memory during an animation sequence, if it lasted too long. The solution, as it turned out, was to take the getGraphics call out of the loop. I was doing a getGraphics and dispose inside the loop originally, but it appears that the operating system resources that were consumed and supposedly released, during this process, weren't exactly.

You can try to use a tool like JProbe (klgroup.com) or OptimizeIt (optimizeit.com) profiler which tells you excatly what the memory is used for (what and how many objects are allocated) at any point of program execution.  JProbe even presents a graph similar to that of NT perfomance monitor.
Avatar of ap9

ASKER


sprinkmeier:  I tried that it actually wasn't calling gc at all (that I could see) before it ran out of memory.  When I put the gc calls in there manually, a ton of messages showed up.  Hmm.

imladris:  Yeah, I found that if I inserted gc() calls in my loop it seemed to do better (but for some reason tends to crash when you exit).  I'm passing a GraphicsContext to several objects (which then draw themselves to that object) in a loop, but I'm not sure how to take it out of the loop (since it is nested very deeply -- it's actually called in a method, and that method is called within a loop that isn't part of the class).  Is the idea to just get the graphics context once, and hold on to it and use it?

msmolyak:  Cool!  Thanks for the info -- I'll look into it.


ap9
ASKER CERTIFIED SOLUTION
Avatar of malexiev
malexiev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ap9

ASKER

Hmm, thanks, I think that's the info I need!