Solved

What causes OutOfMemoryError?

Posted on 1998-10-31
10
187 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
Comment Utility
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
Comment Utility
Yes, the VM is usually restricted to 60M of memory. Try specifying more.
0
 

Author Comment

by:ap9
Comment Utility
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
 

Author Comment

by:ap9
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:imladris
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
Hmm, thanks, I think that's the info I need!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now