3

Possible Duplicate:
Setting JVM heap size at runtime

Is it possible to prevent a program from crashing when it encounters a OutOfMemoryError by increasing the memory allowed to the program?
Can it be done at run time?

Reason for increasing the memory

I was talking a lot of screen shots using java.awt.Robot and after some time my Vector ran out of memory. At 60 BufferedImage it was out.
so 1280 x 800 resolution, 3 byte RGB BufferedImage and 60 images later, the vector was out.
So I guess the memory consumed was
1280 x 800 x 60 x 3 = do the math bytes

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221

4 Answers4

3

Short answer - No. Or at least, not with Hotspot JVMs.

References:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

Ok well you can't actually increase the heapsize, but you could spawn another process with a new heapsize. Have a play with this:

 public class SpawnAndChangeHeap {
     public static void main(String[] args){  

         //Get the jvm heap size.  
        long heapSize = Runtime.getRuntime().totalMemory();
    JOptionPane.showMessageDialog(null, "" + heapSize );  

    if(args.length > 0 && args[0].equals("-spawn")) {

        try {
            Process proc;
                proc = Runtime.getRuntime().exec("cmd.exe /c java -Xms32m -Xmx128m SpawnAndChangeHeap /n");
        }
        catch(Exception e) {System.out.println("something went wrong");  }
    }
    System.exit(0);


     }  
}
Mark
  • 2,423
  • 4
  • 24
  • 40
  • See also [this answer](http://stackoverflow.com/questions/13783086/is-there-a-way-to-increase-java-heap-space-in-the-code-itself/13783276#13783276) which uses the more robust `ProcessBuilder`. – Andrew Thompson Jan 07 '13 at 02:26
2

You may need to think of other solutions other than dynamically changing memory size including

  • allocating a reasonable amount of memory to start with.
  • decreasing the resolution of your captured images (which is what I did for a similar problem by decreasing image size)
  • caching your images to disk when not immediately needed vs. a combination.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    I overcame that by directly encoding the images to a video and writing them to the disk. :) – An SO User Jan 07 '13 at 02:19
  • 1
    to be more specific, I used `Xuggler` API and it has a `IMediaWriter` that transcodes the data and writes it to the disk as a video. All you need to do is supply an extension. It will do the rest – An SO User Jan 07 '13 at 02:22
  • I was going over your profile and I saw that you use AutoIt. I have to study it as apart of my course. Is it really used a lot in day-to-day affairs? – An SO User Jan 07 '13 at 08:26
  • @LittleChild: I don't know AutoIt's usage statistics, but it is quite a handy little tool that can be used in many ways and for many purposes. – Hovercraft Full Of Eels Jan 07 '13 at 13:25
2

I was talking a lot of screen shots using java.awt.Robot and after some time my Vector ran out of memory.

Don't put them in a Vector. There is no need to store them in memory at all.

Instead save each image to either of:

  1. A Zip archive (this gets around creating '1000s of files' - which is itself problematic) with no compression (zip compression does nothing good for images).
  2. A video stream (pointing to a file). See the Movie Maker of the Monte Media Library for a JMF based version that works quite well.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Xuggler did the trick for me. I even had a look at Monte. But the code is a `horrible mess` according to Randelschofer himself. – An SO User Jan 07 '13 at 08:27