简体   繁体   中英

Java Images take up too much memory

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM