Solved

What causes OutOfMemoryError?

Posted on 1998-10-31
10
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to execute a Python program and gather return output in Java 2 62
Chrome and Firefox Java 5 88
ejb mdb examples 1 38
Java array 10 86
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 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