简体   繁体   English

Java映像占用过多内存

[英]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. 它还创建了多个BufferedImages,大约为400x400,其中不超过10个。

The images from file total around 6MB. 来自文件的图像总计约6MB。 Each BufferedImage should be approx 400x400x4=640KB. 每个BufferedImage大约应为400x400x4 = 640KB。

Yet, the memory usage is 500MB! 但是,内存使用量为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? 还是使用更少内存的BufferedImage的替代方案?

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. 在第一种情况下,您应该尝试使用标准Java内存分析器识别泄漏。

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. 现在,您已经确定有很多BufferedImages徘徊,接下来需要做的是弄清楚为什么仍然可以访问它们。 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 参见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. 在进行此类操作时,使用您喜欢的Java IDE查看Java源代码的相关部分很有用。

Is your program a web application ? 您的程序是Web应用程序吗? or you are developing a JRE-like application ? 或者您正在开发类似JRE的应用程序? 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. 在基于Web的应用程序中,我将使用CSS-Sprite图像来解决我的问题,因为它切断了多个HTTP请求,并改善了带宽使用率和负载时的内存使用率。

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. 在JRE应用程序中,应该有一种以相同方式使用图像精灵的方法,即加载图像精灵的400x400偏移量以减少BufferedImage对象的使用并提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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