JavaME-LWUIT图像耗尽了所有内存 [英] JavaME - LWUIT images eat up all the memory

查看:72
本文介绍了JavaME-LWUIT图像耗尽了所有内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LWUIT编写MIDlet,图像似乎消耗了不可思议的内存量.我使用的所有图像都是PNG,都打包在JAR文件中.我使用标准Image.createImage(URL)方法加载它们.该应用程序具有多种形式,每个形式都有几个标签,一个按钮,但是我可以肯定的是,只有活动形式才保留在内存中(我知道它不是很值得信赖,但是Runtime.freeMemory()似乎可以确认这个).

I'm writing a MIDlet using LWUIT and images seem to eat up incredible amounts of memory. All the images I use are PNGs and are packed inside the JAR file. I load them using the standard Image.createImage(URL) method. The application has a number of forms and each has a couple of labels an buttons, however I am fairly certain that only the active form is kept in memory (I know it isn't very trustworthy, but Runtime.freeMemory() seems to confirm this).

该应用程序在240x320分辨率下运行良好,但将其移至480x640并在UI上使用较大的图像开始显示内存不足错误.该应用程序的主要功能是下载远程映像.在达到这一点之前,该应用程序似乎可以正常工作.下载几个PNG并返回到主菜单后,遇到内存不足错误.自然地,我调查了主菜单使用的内存量,这非常令人震惊.它只是带有图像和四个按钮的两个标签.每个按钮都有三个用于style.setIcon,setPressedIcon和setRolloverIcon的图像.图像的大小从15到25KB,但是从每个按钮使用的三张图像中删除了两张(总共8张图像),Runtime.freeMemory()显着减少了1MB的内存使用量.

The application has worked well in 240x320 resolution, but moving it to 480x640 and using appropriately larger images for UI started causing out of memory errors to show up. What the application does, among other things, is download remote images. The application seems to work fine until it gets to this point. After downloading a couple of PNGs and returning to the main menu, the out of memory error is encountered. Naturally, I looked into the amount of memory the main menu uses and it was pretty shocking. It's just two labels with images and four buttons. Each button has three images used for style.setIcon, setPressedIcon and setRolloverIcon. Images range in size from 15 to 25KB but removing two of the three images used for every button (so 8 images in total), Runtime.freeMemory() showed a stunning 1MB decrease in memory usage.

我的看法是,我要么发生了很多内存泄漏(我不认为我有,但并不完全知道可以轻易找到内存泄漏),我在做某件事,这是非常糟糕的图像处理或实际上没有任何问题,我只需要缩小比例即可.

The way I see it, I either have a whole lot of memory leaks (which I don't think I do, but memory leaks aren't exactly known to be easily tracked down), I am doing something terribly wrong with image handling or there's really no problem involved and I just need to scale down.

如果任何人有任何见解,我将不胜感激.

If anyone has any insight to offer, I would greatly appreciate it.

推荐答案

移动设备通常内存不足.因此,您必须使用一些技巧来保存和使用内存.

Mobile devices are usually very low on memory. So you have to use some tricks to conserve and use memory.

我们在我们的项目中遇到了同样的问题,我们就这样解决了.

We had the same problem at a project of ours and we solved it like this.

对于下载的图像: 在您放置图像的地方进行缓存.如果需要图像,请检查它是否在缓存映射中,如果不是,则将其下载并放在其中,如果使用,请使用它.如果内存已满,请删除缓存映射中最旧的图像,然后重试.

for downloaded images: Make a cache where you put your images. If you need an image, check if it is in the cachemap, if it isn't download it and put it there, if it is, use it. if memory is full, remove the oldest image in the cachemap and try again.

对于其他资源图像: 将它们保留在内存中的时间只要您能看到它们,如果看不到它们,请断开引用,gc会为您进行清理.

for other resource images: keep them in memory only for as long as you can see them, if you can't see them, break the reference and the gc will do the cleanup for you.

希望这会有所帮助.

这篇关于JavaME-LWUIT图像耗尽了所有内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆