views:

1000

answers:

2

I'm attempting to create a very large image in Java like so:

BufferedImage bi = new BufferedImage(58240, 1664, BufferedImage.TYPE_INT_RGB);

obviously the image is very large.

Now the issue I'm having is that it seems to work fine 100% on some computers but really slowly on others (and no this has NOTHING to do with specs).

My most major breakthrough came in Eclipse, the IDE refused to actually display the image and instead threw an error on one of the computers which displays the image really slowly (takes a considerable amount of time to resize the image and the like):

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

Another interesting element of the error is that even on the computers it runs slowly on I can resize the window it's in until the paint function is no longer being called, then make it large again and if I do it 'right' it runs with 100% of speed.

Not sure what's going on at all, any ideas?

+2  A: 

The issue is with the heap size of the Java Virtual Machine -- there just isn't enough on the systems that threw the OutOfMemoryError.

If the problem is occurring with systems running the Sun JVM, it is possible to change the heap size of the JVM by using Sun's JVM-specific options.

As of Sun's Java 6, the default values for the heap size is determined by the amount of system memory, but is also can be overridden by the -Xms option which changes the minimum heap size, and -Xmx that changes the maximum heap size. By default minimum heap size is 1/64 the amount of physical memory, and maximum is 1/4 the amount of physical memory.

coobird
I think the "allocate fraction of system memory for heap" is head on. Would be interesting to hear the configuration of those systems described in the question.
Thorbjørn Ravn Andersen
+5  A: 

Your app is running out of memory - if I calculated it correctly, that image takes about 280MB.

Java programs have a maximum amount of memory they're allowed to use (heap space), which is fixed when the JVM is started, and how this limit is set varies between JVM implementation and versions. When you're running out of memory or close to the limit, the JVM will spend a lot of time doing garbage collection, which will slow it down considerably.

It may be that the only thing you have to do is to give the app more heap space; this is done with the -Xmx command line parameter.

Michael Borgwardt
I think it's about 390MB (would have been 280 if each pixel was stored as 3 bytes, but it's stored as an int -> 4 bytes). Also, if garbage collection is the issue here, the new JRE (6 update 14) comes with a new version of the garbage collector called G1 which may pose a great improvement.
laginimaineb
if you aren't setting the max memory, the default is only 128M, isn't it? or did they bump it up recently. Either way, this image is going to be huge, and any image manipulation functions on it are going to be equally expensive in memory costs.
John Gardner
THe default used to be ony 64MB, but it really depends on the JRE version. I think I heard somewhere that it now depends on how much physical RAM is present.
Michael Borgwardt