Java Images take up too much memory

白昼怎懂夜的黑 提交于 2019-12-12 10:21:42

问题


I have a program that uses a lot of images. It loads alot of images from file, and stores them so they are only loaded once. It also creates multiple BufferedImages, about 400x400, there would be no more than 10 of these.

The images from file total around 6MB. Each BufferedImage should be approx 400x400x4=640KB.

Yet, the memory usage is 500MB!

Some of this will be for the rest of the program, but I'm pretty sure that the images are taking up most of the space.

Does anyone know what to do about this? Or an alternative to BufferedImage that uses less memory?


回答1:


It sounds to me like your application (not the library) has a memory leak. In the first instance you should try to identify the leak using a standard Java memory profiler.

EDIT

Now you've identified that there are lots of BufferedImages hanging around, the next thing you need to do is figure out why they are still reachable. Take a heap dump, look at some instances and see how they are connected to a heap root; see http://download.oracle.com/javase/6/docs/technotes/guides/visualvm/heapdump.html

When doing this kind of stuff, it is useful to look at the relevant parts of the Java source code using your favorite Java IDE.




回答2:


Is your program a web application ? or you are developing a JRE-like application ? How do you load up your images ?

In a web-based application, I would use a CSS-Sprite image to solve my problem, because it cut off several HTTP requests and improve both bandwith usage and memory usage on load.

In a JRE application, there should be a way to use an image sprite in the same way, loading an offset of 400x400 of your image sprite to reduce usage of the BufferedImage object and improving performance.



来源:https://stackoverflow.com/questions/3868913/java-images-take-up-too-much-memory

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