- 浏览: 41066 次
- 性别:
- 来自: 济南
文章分类
最新评论
前言:
堆(HEAP)是VM中占用内存最多的部分,通常是动态分配的。堆的大小不是一成不变的,通常有一个分配机制来控制它的大小。比如初始的HEAP是4M大,当4M的空间被占用超过75%的时候,重新分配堆为8M大;当8M被占用超过75%,分配堆为16M大。倒过来,当16M的堆利用不足30%的时候,缩减它的大小为8M大。重新设置堆的大小,尤其是压缩,一般会涉及到内存的拷贝,所以变更堆的大小对效率有不良影响。
我们知道,android程序内存一般限制在16M,当然也有24M的,而android程序内存被分为2部分:native和dalvik: dalvik就是我们平常说的java堆,我们创建的对象是在这里面分配的,而bitmap是直接在native上分配的,对于内存的限制是native+dalvik不能超过最大限制。注:一旦内存分配给Java后,以后这块内存纵然开释后,也只能给Java的施用,这个估计跟java虚拟机里把内存分成好几块进行缓存的原因有关(具体解释见下一段),反正C就别想用到这块的内存了,所以要是Java突然占用了一个大块内存,纵然很快开释了:C能施用的内存 = 16M - Java某一瞬间占用的最大内存。而Bitmap的生成是通过malloc进行内存分配的,占用的是C的内存这个也就说明了,上面所说的4MBitmap无法生成的原因,因为在13M被Java用过后,剩下C能用的只有3M了。
在Davilk中,给一个程序分配的内存根据机型厂商的不同,而不同,现在的大部分的是32M了,而在VM内部会把这些内存分成java使用的内存和 Native使用的内存,它们之间是不能共享的,就是说当你的Native内存用完了,现在Java又有空闲的内存,这时Native会重新像VM申请,而不是直接使用java的。
垃圾回收的几种情况:
GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to perform an allocation. Might be triggered when new objects are being created.
GC_EXPLICIT means that the garbage collector has been explicitly asked to collect, instead of being triggered by high water marks in the heap. Happens all over the place, but most likely when a thread is being killed or when a binder communication is taken down.
There are a few others as well:
GC_CONCURRENT Triggered when the heap has reached a certain amount of objects to collect.
GC_EXTERNAL_ALLOC means that the the VM is trying to reduce the amount of memory used for collectable objects, to make room for more non-collectable.
中文理解(Copy的)
GC_MALLOC, 内存分配失败时触发
GC_CONCURRENT,当分配的对象大小超过384K时触发
GC_EXPLICIT,对垃圾收集的显式调用(System.gc)
GC_EXTERNAL_ALLOC,外部内存分配失败时触发
freed 199K表示本次垃圾收集释放了199K的内存,
53% free 3023K/6343K,其中6343K表示当前内存总量,3023K表示可用内存,53%表示可用内存占总内存的比例。
external 0K/0K,表示可用外部内存/外部内存总量
paused
2ms+2ms,第一个时间值表示markrootset的时间,第二个时间值表示第二次mark的时间。如果触发原因不是GC_CONCURRENT,这一行为单个时间值,表示垃圾收集的耗时时间。
调用垃圾回收的原因:
typedefenum{
GC_FOR_MALLOC,
GC_CONCURRENT,
GC_EXPLICIT,
GC_EXTERNAL_ALLOC,
GC_HPROF_DUMP_HEAP
}GcReason;
垃圾回收调试信息解读(就是LogCat中那些GC信息):
Roughly speaking, the format is [Reason] [Amount Freed], [Heap Statistics], [External Memory Statistics], [Pause Time]
Reason
Robert/yuku already gave info on the meaning of these.
Amount Freed
E.g. freed 2125K
Self explanatory
Heap Statistics
E.g. 47% free 6214K/11719K
These numbers reflect conditions after the GC ran. The "47% free" and 6214K reflect the current heap usage. The 11719K represents the total heap size. From what I can tell, the heap can grow/shrink, so you will not necessarily have an OutOfMemoryError if you hit this limit.
External Memory Statistics
E.g external 7142K/8400K
Note: This might only exist in pre-Honeycomb versions of Android (pre 3.0).
Before Honeycomb, bitmaps are allocated external to your VM (e.g. Bitmap.createBitmap() allocates the bitmap externally and only allocates a few dozen bytes on your local heap). Other examples of external allocations are for java.nio.ByteBuffers.
Pause Time
If it""s a concurrent GC event, there will be two times listed. One is for a pause before the GC, one is for a pause when the GC is mostly done. E.g. paused 3ms+5ms
For non-concurrent GC events, there is only one pause time and it""s typically much bigger. E.g. paused 87ms
解决方法:
第一种:
android不同设备单个进程可用内存是不一样的,可以查看/system/build.prop文件。
# This is a high density device with more memory, so larger vm heaps for it.
dalvik.vm.heapsize=24m
上面heapsize参数表示单个进程可用的最大内存,单如果存在如下参数:
dalvik.vm.heapgrowthlimit=16m
largeheaplimit参数表示单个进程内存被限定在16m,即程序运行过程中实际只能使用16m内存,不过有一个办法可以解决,编辑AndroidManifest.xml中的Application节点,增加属性largeheap="true"参数.
第二种:
VMRuntime.getRuntime().setMinimumHeapSize(NewSize); (修改最小内存)话说没感觉有用。
第三种:
private final static floatTARGET_HEAP_UTILIZATION = 0.75f;
VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);
这种是修改内存使用率,在系统GC的时候可以尽量提高回收的效率。
堆(HEAP)是VM中占用内存最多的部分,通常是动态分配的。堆的大小不是一成不变的,通常有一个分配机制来控制它的大小。比如初始的HEAP是4M大,当4M的空间被占用超过75%的时候,重新分配堆为8M大;当8M被占用超过75%,分配堆为16M大。倒过来,当16M的堆利用不足30%的时候,缩减它的大小为8M大。重新设置堆的大小,尤其是压缩,一般会涉及到内存的拷贝,所以变更堆的大小对效率有不良影响。
我们知道,android程序内存一般限制在16M,当然也有24M的,而android程序内存被分为2部分:native和dalvik: dalvik就是我们平常说的java堆,我们创建的对象是在这里面分配的,而bitmap是直接在native上分配的,对于内存的限制是native+dalvik不能超过最大限制。注:一旦内存分配给Java后,以后这块内存纵然开释后,也只能给Java的施用,这个估计跟java虚拟机里把内存分成好几块进行缓存的原因有关(具体解释见下一段),反正C就别想用到这块的内存了,所以要是Java突然占用了一个大块内存,纵然很快开释了:C能施用的内存 = 16M - Java某一瞬间占用的最大内存。而Bitmap的生成是通过malloc进行内存分配的,占用的是C的内存这个也就说明了,上面所说的4MBitmap无法生成的原因,因为在13M被Java用过后,剩下C能用的只有3M了。
在Davilk中,给一个程序分配的内存根据机型厂商的不同,而不同,现在的大部分的是32M了,而在VM内部会把这些内存分成java使用的内存和 Native使用的内存,它们之间是不能共享的,就是说当你的Native内存用完了,现在Java又有空闲的内存,这时Native会重新像VM申请,而不是直接使用java的。
垃圾回收的几种情况:
GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to perform an allocation. Might be triggered when new objects are being created.
GC_EXPLICIT means that the garbage collector has been explicitly asked to collect, instead of being triggered by high water marks in the heap. Happens all over the place, but most likely when a thread is being killed or when a binder communication is taken down.
There are a few others as well:
GC_CONCURRENT Triggered when the heap has reached a certain amount of objects to collect.
GC_EXTERNAL_ALLOC means that the the VM is trying to reduce the amount of memory used for collectable objects, to make room for more non-collectable.
中文理解(Copy的)
GC_MALLOC, 内存分配失败时触发
GC_CONCURRENT,当分配的对象大小超过384K时触发
GC_EXPLICIT,对垃圾收集的显式调用(System.gc)
GC_EXTERNAL_ALLOC,外部内存分配失败时触发
freed 199K表示本次垃圾收集释放了199K的内存,
53% free 3023K/6343K,其中6343K表示当前内存总量,3023K表示可用内存,53%表示可用内存占总内存的比例。
external 0K/0K,表示可用外部内存/外部内存总量
paused
2ms+2ms,第一个时间值表示markrootset的时间,第二个时间值表示第二次mark的时间。如果触发原因不是GC_CONCURRENT,这一行为单个时间值,表示垃圾收集的耗时时间。
调用垃圾回收的原因:
typedefenum{
GC_FOR_MALLOC,
GC_CONCURRENT,
GC_EXPLICIT,
GC_EXTERNAL_ALLOC,
GC_HPROF_DUMP_HEAP
}GcReason;
垃圾回收调试信息解读(就是LogCat中那些GC信息):
Roughly speaking, the format is [Reason] [Amount Freed], [Heap Statistics], [External Memory Statistics], [Pause Time]
Reason
Robert/yuku already gave info on the meaning of these.
Amount Freed
E.g. freed 2125K
Self explanatory
Heap Statistics
E.g. 47% free 6214K/11719K
These numbers reflect conditions after the GC ran. The "47% free" and 6214K reflect the current heap usage. The 11719K represents the total heap size. From what I can tell, the heap can grow/shrink, so you will not necessarily have an OutOfMemoryError if you hit this limit.
External Memory Statistics
E.g external 7142K/8400K
Note: This might only exist in pre-Honeycomb versions of Android (pre 3.0).
Before Honeycomb, bitmaps are allocated external to your VM (e.g. Bitmap.createBitmap() allocates the bitmap externally and only allocates a few dozen bytes on your local heap). Other examples of external allocations are for java.nio.ByteBuffers.
Pause Time
If it""s a concurrent GC event, there will be two times listed. One is for a pause before the GC, one is for a pause when the GC is mostly done. E.g. paused 3ms+5ms
For non-concurrent GC events, there is only one pause time and it""s typically much bigger. E.g. paused 87ms
解决方法:
第一种:
android不同设备单个进程可用内存是不一样的,可以查看/system/build.prop文件。
# This is a high density device with more memory, so larger vm heaps for it.
dalvik.vm.heapsize=24m
上面heapsize参数表示单个进程可用的最大内存,单如果存在如下参数:
dalvik.vm.heapgrowthlimit=16m
largeheaplimit参数表示单个进程内存被限定在16m,即程序运行过程中实际只能使用16m内存,不过有一个办法可以解决,编辑AndroidManifest.xml中的Application节点,增加属性largeheap="true"参数.
第二种:
VMRuntime.getRuntime().setMinimumHeapSize(NewSize); (修改最小内存)话说没感觉有用。
第三种:
private final static floatTARGET_HEAP_UTILIZATION = 0.75f;
VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);
这种是修改内存使用率,在系统GC的时候可以尽量提高回收的效率。
发表评论
-
linux_shell 特殊符号的介绍
2013-11-28 19:45 0#:注释,行首以#开头为注释 ... -
Ubuntu挂载新硬盘
2013-09-12 18:00 3781、加挂硬盘 sudo hdparm - ... -
U 盘安装系统
2013-06-16 10:16 591[align=left][size=small][size=x ... -
git-am 和 format-patch 的使用
2013-04-07 20:29 1993git format-patch:当你想给一个开源项目(例如R ... -
虚拟机网络问题
2013-02-25 16:41 513如果WIN7 虚拟机重启后,网络设置没有了。或者是一直断开。这 ... -
ubuntu上配置git服务器
2013-02-07 10:06 6061 需求 硬件需求:一台Ubuntu或者debi ... -
minicom 设置
2013-01-17 14:49 0安装配置minicom ------------------- ... -
Ubuntu10.10 Sun java6 sdk install
2013-01-04 10:28 0Ubuntu 11.10 install Sun Java6 ... -
Zim
2012-12-26 13:48 0Zim源的安装 sudo add-apt-reposito ... -
cifs and smba
2012-12-17 10:59 0linux cifs自动挂载远程win ... -
source: not found
2012-12-14 09:52 0现象: shell脚本中source aaa.sh时提示 so ... -
java url 当中的特殊字符
2012-12-07 17:53 0java处理url中的特殊字符(如&,%...) UR ... -
eclipse 导入jar问题
2012-12-05 19:14 0如果eclipse 报java.awt.*找不到了。。那么估计 ... -
韩信、萧何、张良
2012-12-02 22:57 0萧何知人善用,有政治 ... -
文件系统
2012-11-16 09:35 0二,NFS(network file system)用于Lin ... -
eclipse 环境配置
2012-11-08 22:29 654如果在安装了了eclipse 后,再安装adt插件。然后的wi ... -
ubuntu 下面安装XBMC 设置中文
2012-10-30 23:06 14731.在微软的windows操作系统c:windowsfonts ... -
刷机 s
2012-11-15 19:33 685顾名思义,recovery.img 是为了恢复系统的,相对于普 ...
相关推荐
基本上解决了OOM问题 如果 方便可以直接引用BitmapManager类到 项目中使用 解决blog 地址http://www.cnblogs.com/liongname/articles/2345087.html
android加载大量图片内存溢出的三种解决办法
Android webview 内存泄露的解决方法 最近在activity嵌套webview显示大量图文发现APP内存一直在涨,没法释放内存,查了很多资料,大概是webview的一个BUG,引用了activity导致内存泄漏,所以就尝试传递...
android 图片下载 有效解决内存溢出问题 android 图片下载 有效解决内存溢出问题
Android 内存溢出的一些解决办法
OOM全称”Out Of Memory”,即内存溢出。 内存溢出已经是软件开发历史上存在了近40年的“老大难”问题。在操作系统上运行各种软件时,软件所需申请的内存远远超出了物理内存所承受的大小,就叫内存溢出。 内存溢出...
针对Android应用中Gallery内存溢出的解决方案
一个android小工程,里面有我自己写的一个工具类,可以轻松避免加载图片列表内存溢出,分享给需要的朋友,工程使用android 1.6SDK完成
kettle内存溢出(Java heap space)以及解决方法
TOMCAT内存溢出之解决方法
比较傻瓜式的 解决了 listview 中图片太多 引起的 内存溢出问题。代码很简单 非常容易掌握
myeclipse内存溢出解决方法 非常好用
Android有效解决加载大图片时内存溢出的问题
Android防止内存溢出浅析.zip
解决内存溢出的问题,在使用myeclipse时候,经常遇到内存溢出的问题。
但是如果程序在一次操作中打开几个M的文件,那么通常会出现下面的错误信息。 02-04 21:46:08.703: ERROR/dalvikvm-heap(2429): 1920000-byte external allocation too large for this process
我们经常会使用phpExcel导入或导入xls文件,但是如果一次导出数据比较大就会出现内存溢出错误,下面我来总结解决办法
内存溢出配置,内存溢出配置内存溢出配置内存溢出配置
Android防止内存溢出浅析
内存溢出的解决方法 首先把c:\windows\system32\ufcomsql\ufrtprn.ocx这个文件复制到其他地方,再用正常的文件(下面的附件)替换一下, 然后重新注册,注册如下.......