在循环中创建位图时出现内存泄漏
我有一个地图应用程序。在我的代码中的某个地方,我将数据从数据库渲染到画布中。 我遇到了“内存不足”异常,我不知道如何避免它。
这是相关方法。使用 bitmapfactory 创建位图时出现异常。
private static void _renderImage(Canvas g, Point[] points, RImageData imageData,
RMapView mapView) {
Bitmap image = (Bitmap)imageData.image;
Paint paint = new Paint();
if(image == null) {
image = BitmapFactory.decodeByteArray(imageData.getImageBytes(), 0,
imageData.getImageBytes().length);
imageData.image = image;
}
g.drawBitmap(image, points[0].x, points[0].y, paint);
}
我尝试过回收图像,但画布却无法处理回收的位图。
任何解决方案将不胜感激。
I have an map app. Somewhere in my code I am rendering data from database into canvas.
I ran into "out of memory" exception and I cant figure out how to avoid it.
Here is the relevant method. I get exception when creating bitmap with bitmapfactory.
private static void _renderImage(Canvas g, Point[] points, RImageData imageData,
RMapView mapView) {
Bitmap image = (Bitmap)imageData.image;
Paint paint = new Paint();
if(image == null) {
image = BitmapFactory.decodeByteArray(imageData.getImageBytes(), 0,
imageData.getImageBytes().length);
imageData.image = image;
}
g.drawBitmap(image, points[0].x, points[0].y, paint);
}
I have tried recycling the image, but then the canvas coplains it cant work with recycled bitmaps.
Any solution would be much apreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议有一个位图缓存。即使回收预蜂窝上的图像也需要时间来释放内存(位图数据存储在不由 dalvik 直接管理的本机内存中)。这是位图缓存的示例。请根据您的需要进行调整。
I would suggest having a bitmap cache. Even recycling images on pre-honeycomb takes time to free memory (Bitmap data is stored in native memory that is not directly managed by dalvik). Here is a sample of a bitmap cache. Please adjust it to your needs.
我认为这个想法是你应该“回收”位图,然后将其放在地板上供垃圾收集器处理。
以下是 javadoc 关于
recycle()
的说法:I think that the idea is that you are supposed to "recycle" the bitmap and then drop it on the floor for the garbage collector to deal with.
Here's what the javadoc says about
recycle()
: