如何辨别真假如何接近你的活动是为了达到它的内存限制? [英] How to tell how close your activity is to reaching its memory limit?

查看:200
本文介绍了如何辨别真假如何接近你的活动是为了达到它的内存限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个图形设计的应用程序为Android用户可以在其中添加多个图像文件,其中每个图像存储为一个位图对象。每个位图有大约的800x400像素的尺寸,并使用ARGB8888像素格式(即〜的1.5Mb个)。

I'm writing a graphic design application for Android where the user can add several images to a document, where each image is stored as a Bitmap object. Each bitmap has roughly a dimension of 800x400 pixels and uses ARGB8888 pixel format (i.e. ~1.5Mb each).

我知道,大多数第一代的Andr​​oid设备有16Mb的堆限制,这个限制是24MB和较新的手机大。我也知道,位图存储在外部分配的,但我很困惑,什么这意味着什么是。

I'm aware that most of the first generation Android devices have a 16Mb heap limit and this limit is 24Mb and larger for newer phones. I'm also aware that bitmap memory is allocated externally, but I'm confused and what the implications of this is.

我的问题是:我如何才能加入的时候告诉在运行一个新的位图将让我太靠近内存限制

My question is: How can I tell at runtime when adding a new Bitmap will get me too close to the memory limit?

在有人建议不要用那么多的记忆,我知道,一个选择我是限制用户可以有多少位图创建这样的,我知道这个限制是安全的最基本的Andr​​oid手机。不过,我想对手机有更大的内存限制,以支持更多的位图和/或更大的位图。

Before someone suggests "don't use that much memory", I know that one option I have is to limit how many Bitmaps the user can create such that I know this limit is safe for the most basic Android phones. However, I'd like for phones with a bigger memory limit to support more bitmaps and/or bigger bitmaps.

我知道分配位图时检查内存不足的异常。然而,会有一些情况下,我只带了刚刚足够的存储空间来分配多一个位图。在此之后,整个应用程序将是不稳定的,因为即使分配的小东西,例如字符串可能会导致内存不足异常。这是我想避免的。

I know to check for OutOfMemory exceptions when allocating bitmaps. However, there will be some situations where I've only got just enough memory left to allocate one more bitmap. After this point, the whole application will be unstable because even allocating small things like strings could cause an OutOfMemory exception. This is something I want to avoid.

我不知道如何界定太接近内存限制,但我怀疑像不分配超过一半的可用内存为位图将工作确定为我的其它数据结构我店在内存小了的。

I'm not sure how to define "too close to the memory limit", but I suspect something like "don't allocate more than half of your available memory to bitmaps" would work OK as my other data structures I store in memory are small in comparison.

推荐答案

使用这种方法记录使用的内存,但是我已经张贴了关于

Use this method to log used memory, but I already posted that on

<一个href="http://stackoverflow.com/questions/3238388/android-out-of-memory-exception-in-gallery/3238945#3238945">http://stackoverflow.com/questions/3238388/android-out-of-memory-exception-in-gallery/3238945#3238945

请参阅该职位作进一步的解释。

see that post for further explanation.

public static void logHeap(Class clazz) {
    Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/1048576.0);
    Double available = new Double(Debug.getNativeHeapSize())/1048576.0);
    Double free = new Double(Debug.getNativeHeapFreeSize())/1048576.0);
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);

    Log.d(APP, "debug. =================================");
    Log.d(APP, "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free) in [" + clazz.getName().replaceAll("com.myapp.android.","") + "]");
    Log.d(APP, "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576.0)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576.0))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576.0)) +"MB free)");
    System.gc();
    System.gc();

    // don't need to add the following lines, it's just an app specific handling in my app        
    if (allocated>=(new Double(Runtime.getRuntime().maxMemory())/1048576.0)-MEMORY_BUFFER_LIMIT_FOR_RESTART)) {
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}

这篇关于如何辨别真假如何接近你的活动是为了达到它的内存限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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