tags:

views:

98

answers:

6

I have one main class that contains 5 buttons each link to a program/package. Each package runs a jmf program that capture images from a webcam and it also loads about 15 images from file.

The 1st program to load(regardless of which button i press) always runs correctly. But When i run a program after the 1st program ends, java.lang.OutOfMemoryError: java heap space occurs.

Im not sure if java can't handle all of our images or if it has something to do with jmf image capture.

+1  A: 

How much memory are you giving to your JVM? You can give it more using the following: -Xmx1024m (for 1GB, adjust as necessary)

This assumes that you don't have some memory leak in your program. I don't know anything about JMF, this is just general advice for Out of Memory errors.

danben
+3  A: 

Maybe you should give more memory to your JVM (-Xmx512m on the command line could be a good start),

then, if it solves the problem, investigate why your programs consumes so much memory.

The use of sun diagnostic tools like jvisualvm could be helpful.

Laurent K
+2  A: 

Increase the Java maximum memory and re-rerun. If you still see OOM's, you may have a leak. To increase the max memory, append -Xmx<new heap size>m to your command line.

Example:

java -Xmx1024m Foo
Amir Afghani
A: 

JVMs run with a limited amount of maximum memory available to them. This is a little counterintuitive and trips a lot of people up (I can't think of many similar environments).

You can increase the max memory the JVM takes by specifying

java -Xmx128m ...

or similar. If you know in advance that you're going to consume that amount of memory, use

java -Xms128m ...

to specify the memory that the JVM will allocate at startup. Note the -Xms vs -Xmx !

Brian Agnew
A: 

Try to check, if you still have some references around which prevent the first package/program to be garbage-collected.

When the launcher has detected that the first program has ended, set all references to the first program and maybe objects retrieved from it to NULL to allow the JVM to reclaim the memory again and have it ready for the second launch.

Kosi2801
A: 

Java uses 64 MByte heap space by default. An alternative to the other suggestions (increasing heap space to 512M or 1024M) is to start separate JVMs for the controller and the 5 applications. Then if one of your JMF applications crashes (due to insufficient memory), the controller and the other apps are still running.

(this will only work if the applications and the controller are completely decoupled - otherwise, just increase the heap size and dispose all media as soon as you don't need it anymore to prevent from memory leaks)

Andreas_D