Creating very large image files with BufferedImage, strange issues depending on compilation and computer

江枫思渺然 提交于 2019-12-08 04:11:25

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/961795/creating-very-large-image-files-with-bufferedimage-strange-issues-depending-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!