Solved

What causes OutOfMemoryError?

Posted on 1998-10-31
10
189 Views
Last Modified: 2006-11-17

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
0
Comment
Question by:ap9
10 Comments
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1226750
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?

0
 
LVL 8

Expert Comment

by:diakov
ID: 1226751
Yes, the VM is usually restricted to 60M of memory. Try specifying more.
0
 

Author Comment

by:ap9
ID: 1226752
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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ap9
ID: 1226753
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
0
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1226754
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

0
 
LVL 16

Expert Comment

by:imladris
ID: 1226755
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.

0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1226756
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.
0
 

Author Comment

by:ap9
ID: 1226757

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
0
 
LVL 1

Accepted Solution

by:
malexiev earned 100 total points
ID: 1226758
As I understood, you try to do animation. There's a great problem with that (that's "createImage" method). You can see more information in www.javasoft.com in Java Developer Connection. There're some bug reports (in Bug Parade) where the bug is called
"createImage causes memory leaks" or something like that. You can see a lot of comments there. They complains why it works on Sparc and not on WIN 95/NT.

We have the same problem with image creating and try to workaround.
And I think your problem is the same bug.
0
 

Author Comment

by:ap9
ID: 1226759
Hmm, thanks, I think that's the info I need!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question