如何在我的code动态清空或清除堆内存 [英] How to empty or clear heap memory dynamically in my code

查看:353
本文介绍了如何在我的code动态清空或清除堆内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我使用简单的库和的CoverFlow两个 我有我的形象的CoverFlow的图片库中的CoverFlow点击我重定向到下一次的内容全屏画廊,我甚至可以滚动我全屏画廊的活动; 但是当我把更多数量的图像或高分辨率的图像在我的应用程序就获得力量为位图的大小关闭,由于超过VM预算

In my application i am using simple gallery and the coverflow both i have my coverflow image gallery on image clicked in coverflow i am redirected to next activity that contents full screen gallery and i can even scroll my fullscreen gallery ; but when i put more amount of image or high resolution images in my app it get force closed due to Bitmap size exceeds VM budget

所以我想清除堆内存每次我完成我的Cover Flow和画廊,所以我可以加载在我的应用程序的任何数量或任何分辨率的图像 因此,如果任何人可以帮助我? 如何每次清除/空堆内存,当我动态完成我的活动我的code? 我已经尝试回收和的System.gc方法

so i want to clear heap memory every time i finish my cover flow and gallery so i can load any amount or any resolution image in my app so if any one can help me out ... How to clear/empty heap memory every time when i finish my activity dynamically in my code? i have already tried recycle and System.gc method

推荐答案

您不能清除堆。虚拟机就会自动在你的对象不再被引用。

You can't "clear the heap". the VM will do that automatically when your objects are no longer referenced.

话虽这么说,位图是一个艰难的事情机器人。有有限的内存,当图像是德coded到一个位图可占用比COM pressed图像格式本身更大量的内存。

That being said, bitmaps are a tough thing in android. There's limited memory, and when an image is decoded into a bitmap it can take up a lot more memory than the compressed image format itself.

有没有简单的答案,您的解决方案。即使你正确地做的一切,你可以简单地运行内存。话虽这么说,这里有一些提示:

There's no easy answer to your solution. Even if you do everything correctly, you may simply be running out of memory. That being said, here are some tips:

  1. 使用 Bitmap.release()。位图是特殊的,因为它们被分配在本地栈(相对于VM)堆。的javadoc是有点模糊,说你通常不需要调用此,但在我的经验,这个线索到虚拟机,你是与内存支持位图做的是非常重要的。 编辑:作为Android的3.0(API级别11),像素数据存储在Dalvik的堆以及相关的位图

  1. Use Bitmap.release(). bitmaps are special in that they are allocated on the native stack (as opposed to VM) heap. The javadocs are a little fuzzy, saying that you don't normally need to call this, but in my experience this "clue" to the VM that you are done with the memory backing the bitmap is important. As of Android 3.0 (API level 11), the pixel data is stored on the Dalvik heap along with the associated bitmap.

装载位图到内存中的缩放的。这里有一个博客文章的话题。你只有24MB堆在某些设备上,当它加载到一个位图对象的高清晰度的图像可能用尽一切。有周围没有办法,所以你必须加载缩放。

Load bitmaps into memory scaled. Here's a blog post on the topic. You only get 24MB heap on some devices and a high-res image could exhaust all of that when it's loaded into a bitmap object. There's no way around that, so you have to load it scaled.

不要叫的System.gc()作为另一个回答说。所建立的GC算法的人比你和我聪明;他们知道自己在做什么。你将永远不会得到一个OOME时,有内存,可以通过GC被释放 - GC将永远给你一个OOME之前运行

Don't call System.gc() as another answer said. The folks that built the GC algorithm are smarter than you and me; they know what they are doing. You will never get an OOME when there's memory that can be freed through a GC - the GC will always run before giving you an OOME.

这其中的明显,但要确保你不将引用位图时,他们并不需要任何更长的时间。

This one's obvious, but make sure you are not holding references to bitmaps when they are not needed any longer.

最后,而这一次吮吸,Android不这样做堆压缩。这里有一个<一个href="http://stackoverflow.com/questions/6349943/android-bitmaps-recycling-and-heap-fragmentation">SO问题我贴在它前一段时间,没有令人满意的答案。简单地说,Android的从未压缩堆,从而为您的应用程序保持分配大的,甚至是中等大小,块位图,你最终会风在一个情况下,而你是不是内存,没有足够大的连续大块的内存为您分配,你会得到OOME。的的只有的解决这个方式,因为我在我的问题写的,是当用户退出杀应用程序的过程。下次本作启动慢,但它保证了品牌新的堆。不要误会我的意思,我不能相信这是正确的事情,但没有人想出了一个更好的解决方案。

Finally, and this one sucks, Android does not do heap compaction. Here's a SO question I posted on it some time ago, with no satisfactory answer. In a nutshell, Android never compacts the heap, so as your app keeps allocating big, or even medium size, chunks for bitmaps, you eventually wind up in a situation where, while you aren't out of memory, there's no large enough contiguous chunk of memory for your allocation, and you get OOME. The only way around this, as I wrote in my question, is to kill the app's process when the user quits. This make startup slower the next time, but it guarantees a brand new heap. Don't get me wrong, I can't possibly believe this is the right thing to do, but no one has come up with a better solution.

这篇关于如何在我的code动态清空或清除堆内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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