views:

71

answers:

2
final BufferedImage img = new BufferedImage(3500, 2480, BufferedImage.TYPE_BYTE_INDEXED);
final Graphics2D g2D = img.createGraphics();
g2D.setBackground(Color.white);
g2D.clearRect(0, 0, width, height);

(full code in this question).

This code results in a Java Heap Space exception although I've set the jvm attribute to 3GB. Could that be the problem?

Anyway isn't it possible to draw the image with less memory? I'm after an A4-size BMP for printing, only with text. Most of the time it's black-and-white only, while at some times I'll need to use blue, pink, and gray too.

A: 

Could you chunkify your picture? That is divide the creation of it into smaller chunks to keep the memory footprint down while processing. Finally, when you're ready to merge, you just merge smaller chunks one at a time. Then you'd be left with the originally sized picture without needing to worry about RAM caps.

wheaties
Could you please explain how to merge smaller chunks programatically without getting an OOME?
mklhmnn
@mklhmnn you'll read the file in binary and write the results back to the final destination. Since you're only holding X bytes in memory you can limit how much overhead you require. It's pretty much the same technique that people use when doing large scale matrix math, large file work, and anything where the entire object placed into memory wouldn't fit.
wheaties
So I create n (say 4) 4 files and then write them into a single one. How about creating one bufferedimage, writing it to the file, and then create another one and append it to the file, n times? The only thing is that I don't know how to append to an image using ImageIO.java. Do you have code that handles this problem already?
simpatico
A: 

There is something very fishy about the facts stated in your question. Unless I'm mistakened, that BufferedImage uses 1 byte per pixel, and therefore 3500 x 2480 bytes ... around 10Mbytes. But you claim to have set the heap size to 3Gbytes.

What JVM options did you actually use?

Stephen C
Env.MAVEN_OPTS=-Xmx1024M (1GB now)
simpatico
That's the setting for the maven build. What do you use when you run your application?
Stephen C
It's the settings for: install nbm:run-platform , which is how I run it.
simpatico
I think you are actually setting the options for the JVM that runs maven NOT the child JVM that runs your application.
Stephen C
I think you to use the "additionalArguments" parameters as described here: http://mojo.codehaus.org/nbm-maven-plugin/run-platform-mojo.html
Stephen C
I'm pretty sure the problem emerged moving to maven 3, now that I think of it. Also, the edition executing from a .jnlp setting the memory works. Where can I learn more about the maven jvm and child?
simpatico
I imagine this is specific to the plugin you are using. Try reading the documentation for the nbm plugin (e.g. the link above!), and if that fails check the mailing lists or Google it.
Stephen C
have found where to set it. I've posted the way to at http://forums.netbeans.org/topic32681.html
simpatico