位置: 编程技术 - 正文

Android内存泄露监控(android内存泄露 工具)

编辑:rootadmin

推荐整理分享Android内存泄露监控(android内存泄露 工具),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:android内存泄露会怎么样,android内存泄露原因及优化,android内存泄露排查,android内存泄露的几种情况,android内存泄露如何解决,android内存泄露原因及优化,android内存泄露如何解决,android内存泄露的几种情况,内容如对您有帮助,希望把文章链接给更多的朋友!

转载: of Memory,而被系统Kill。adb shell getprop | grep dalvik:

[dalvik.vm.heapsize]: [m]

内存泄露-人怕出名猪怕壮

LMK(LowMemoryKiller)会周期性的运行,工作时,首先根据下面的&#;确定当前的警戒级数,高于警戒级数的进程是待杀的范围。若找到多个,则把占用进程最大的进程,发送SIGKILL,杀掉该进程。

而Android也会在E:SrcIceCreamSandwichframeworksbaseservicesjavacomandroidserveramActivityManagerService.java中去动态的调整每一个app的Level。

/system/core/rootdir/init.rc# Define the oom_adj values for the classes of processes that can be killed by the kernel. setprop ro.FOREGROUND_APP_ADJ 0 setprop ro.VISIBLE_APP_ADJ 1 setprop ro.PERCEPTIBLE_APP_ADJ 2 setprop ro.HEAVY_WEIGHT_APP_ADJ 3 setprop ro.SECONDARY_SERVER_ADJ 4 setprop ro.BACKUP_APP_ADJ 5 setprop ro.HOME_APP_ADJ 6 setprop ro.HIDDEN_APP_MIN_ADJ 7 setprop ro.EMPTY_APP_ADJ # Define the memory thresholds at which the above process classes will be killed. in pages (4k). setprop ro.FOREGROUND_APP_MEM setprop ro.VISIBLE_APP_MEM setprop ro.PERCEPTIBLE_APP_MEM setprop ro.HEAVY_WEIGHT_APP_MEM setprop ro.SECONDARY_SERVER_MEM setprop ro.BACKUP_APP_MEM setprop ro.HOME_APP_MEM setprop ro.HIDDEN_APP_MEM setprop ro.EMPTY_APP_MEM =======================================================Android中如何查看内存

(一)DDMS 的Heap Dump

1) Data Object:java object.

2) Class Object:object of type Class, e.g. what you'd get from java.lang.String.class or myObject.getClass( ).3) 1,2,4,8-byte array:Number of bytes per entry. 1-byte array: byte, boolean 2-byte array: char, short 4-byte array: float, int 8-byte array: double, long

4) non-Java object:A non-Java Object is a piece of memory that isn't actually accessible from code written in Java. Essentially it's a blob of stuff that got stuck on the virtual heap but has no meaning to interpreted code. Shouldn't be much of that.

(二)如果你想查看所有进程的内存使用情况,可以使用"adb shell procrank"命令。

VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)

VSS (reported as VSZ from ps) is the total accessible address space of a process. This size also includes

memory that may not be resident in RAM like mallocs that have been allocated but not written to.

VSS is of very little use for determing real memory usage of a process.RSS is the total memory actually held in RAM for a process. RSS can be misleading,

because it reports the total all of the shared libraries that the process uses,

even though a shared library is only loaded into memory once regardless of how many processes use it.

RSS is not an accurate representation of the memory usage for a single process.PSS differs from RSS in that it reports the proportional size of its shared libraries,

i.e. if three processes all use a shared library that has pages,

that library will only contribute pages to the PSS that is reported for each of the three processes.

PSS is a very useful number because when the PSS for all processes in the system are summed together,

that is a good representation for the total memory usage in the system.

When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to

the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading,

because when a process is killed, PSS does not accurately represent the memory returned to the overall system.USS is the total private memory for a process, i.e. that memory that is completely unique to that process.

USS is an extremely useful number because it indicates the true incremental cost of running a particular process.

When a process is killed, the USS is the total memory that is actually returned to the system.

USS is the best number to watch when initially suspicious of memory leaks in a process.Android内存泄露监控(android内存泄露 工具)

(三)如果只想查看单个进程,可以使用“adb shelldumpsys meminfo”,该命令后面要加上进程的名字,以确定是哪个进程。

dalvikPrivateDirty:The private dirty pages used by dalvik。

dalvikPss :The proportional set size for dalvik.dalvikSharedDirty :The shared dirty pages used by dalvik.nativePrivateDirty :The private dirty pages used by the native heap.nativePss :The proportional set size for the native heap.nativeSharedDirty :The shared dirty pages used by the native heap.otherPrivateDirty :The private dirty pages used by everything else.otherPss :The proportional set size for everything else.otherSharedDirty :The shared dirty pages used by everything else.

PrivateDirty, which is basically the amount of RAM inside the process that can not be paged to disk (it is not backed by the same data on disk),and is not shared with any other processes.Another way to look at PrivateDirty is the RAM that will become available to the systemwhen that process goes away (and probably quickly subsumed into caches and other uses of it).In one word,Priv Dirty(Total) is Uss。

Interesting thing to note here: Pss and Uss are slightly (or more than slightly) different than what we saw in meminfo. Why is that? Well procrank uses a different kernel mechanism to collect its data than meminfo does, and they give slightly different results. Why is that? Honestly I haven't a clue.

(四)使用"adb shell cat /proc/meminfo" 命令,该方式只能得出系统整个内存的大概使用情况。

(五)MAT Eclipse->Help->Install New Softwares

If your ADT is not gelivable, then you can get the .hprof file and use hprof-conv to convert it. The hprof-conv tool converts the HPROF file that is generated by theAndroid SDK tools to a standard format. hprof-conv is under android sdk tools folder. hprof-conv src.hprof dst.hprof

Shallow Size and Retained Size

Shallow size就是对象本身占用内存的大小,不包含对其他对象的引用Retained size是该对象自己的shallow size,加上只能从该对象直接或间接访问到对象的shallow size之和。换句话说,retained size是该对象被GC之后所能回收到内存的总和。

Shallow size of an object is the amount of memory allocated to store the object itself, not taking into account the referenced objects. Shallow size of a regular (non-array) object depends on the number and types of its fields. Shallow size of an array depends on the array length and the type of its elements (objects, primitive types). Shallow size of a set of objects represents the sum of shallow sizes of all objects in the set.

Retained size of an object is its shallow size plus the shallow sizes of the objects that are accessible, directly or indirectly,only from this object. In other words, the retained size represents the amount of memory that will be freed by the garbage collector when this object is collected.

To better understand the notion of the retained size, let us look at the following examples:

In order to measure the retained sizes, all objects in memory are treated as nodes of a graph where its edges represent references from objects to objects. There are also special nodes -GC root objects, which will not be collected byGarbage Collector at the time of measuring (read more about GC roots).

The pictures below show the same set of objects, but with varying internal references.

Figure 1:Figure 2:

Let us consider obj1.As you can see, in both pictures we have highlighted all of the objects that are directly or indirectly accessed only byobj1. If you look at Figure 1, you will see that obj3 is not highlighted, because it is also referenced by a GC root object. On Figure 2, however, it is already included into the retained set, unlike obj5, which is still referenced by GC root.

Thus, the retained size of obj1 will represent the following respective values:

For Figure 1: the sum of shallow sizes of obj1, obj2 and obj4For Figure 2: the sum of shallow sizes of obj1, obj2, obj3 andobj4

Looking at obj2, however, we see that its retained size in the above cases will be:

For Figure 1: the sum of shallow sizes of obj2 and obj4For Figure 2: the sum of shallow sizes of obj2, obj3 and obj4

In general, retained size is an integral measure, which helps to understand the structure (clustering) of memory and the dependencies between object sub-graphs, as well as find potential roots of those sub-graphs.

Dominator Tree

An object A dominates on an object B if all the paths toobject B pass through object A.Using the "dominates" relationship we can create a dominatortree out of the graphof objects in memory.

(六)使用“adb shell ps x”命令,该方式主要得到的是内存信息是VSIZE 和RSS。由于RSS的价&#;不是很大,所以一般不用。

Android内存泄露的原因

(一)释放对象的引用,误将一个本来生命周期短的对象存放到一个生命周期相对较长的对象中,也称“对象游离“。隐蔽的内部类(Anonymous Inner Class):[html] view plaincopymHandler = new Handler() { @Override public void handleMessage(Message msg) { .... } };

相当于:

[html] view plaincopyclass MyHandler implements Handler { .... } mHandler = new MyHandler(); 类&#;的还有Listener,Observer,Receiver等。由于内部类隐含拥有一个外部类对象的引用,所以千万要注意内部类instance的声明周期。最好不要将一个内部类的instance暴漏出去,除非使用者采用weak reference引用内部类的instance。例如,一些Handler,Listener在做为返回&#;暴漏出去时,千万注意使用weak reference。注意:一些系统提供的类偷偷地将自己暴漏出去了,如FileObserver,会将自己暴漏到自己内部开的线程去,不过还好它使用的是weak reference。但是过分的是BroadcastReceiver,一旦我们使用Context的registerReceiver 之后,系统( ActivityManagerNative )就会有一个它的强引用,除非我们显式调用unregisterReceiver。

结论:任何提供了开关函数的类,都必须保证显式地调用它们的开关函数。

(二) 构造Adapter时,没有使用缓存的 convertView,从而造成系统创建了大量的view而没有来得及回收。

public View getView(int position, View convertView, ViewGroup parent)

(三)对于资源性对象在不使用的时候,应该调用它的close()函数,将其关闭掉,然后才置为null。资源性对象,比如(Cursor等)往往都用了一些缓冲,我们在不使用的时候,应该及时关闭它们,以便它们的缓冲及时回收内存。它们的缓冲不仅存在于java虚拟机内,还存在于java虚拟机外。如果我们仅仅是把它的引用设置为null,而不关闭它们,往往会造成内存泄露。

(四)都是线程惹的祸

由于线程的执行时间是未知的,所以线程所持有的外部对象,一般要使用weak reference,除非你肯定它们的生命周期。另外,向其它线程post Runnable或者send Message时也需要注意Runnalbe内部使用的变量和Message中的内容。(五)AsyncTask-隐藏的故事Asynctask使用了一个静态的ThreadPoolExecutor,有三个参数是系统固定的:CORE_POOL_SIZE:5MAXIMUM_POOL_SIZE:QUEUE_CAPACITY: 当一个AsyncTask被execute之后:1.如果线程池中线程数目小于CORE_POOL_SIZE ,就直接开线程;2.如果队列不满,就被加入队列中;3.如果队列已满,就直接开线程执行;4.如果线程池中线程数目大于MAXIMUM_POOL_SIZE,就会Exception。Tricky的地方在于AsncTask的Cancel操作。1.不一定都能成功,例如while(true){}就无法被cancel;2.如果AsyncTask还在Queue中,那么cancel操作并不会将它从Queue中remove,而仅仅是设置了flag,最终当它被thread执行时,判断该flag,如果false,就不会执行doBackground的代码。悲剧剧情:你在队列中,正在执行的几个哥们都block了,你就一直呆着吧。(六)Bitmap-不得不说的故事Bitmaps in Android are created innative memory, not on the VM heap, so the actual Bitmap object on the VM heapis very small as it doesn't contain any actual bitmap data.一般情况下,我们都是按需索图,没必要把原始图片都搞进来,所以我们可以先查询原始图片大小,在做scale:1. 查询原始图片大小,通过BitmapFactory.Options中的属性inJustDecodeBounds来做,If set to true, the decoder will return null (nobitmap), but the out... fields will still be set, allowing the caller to querythe bitmap without having to llocate the memory for its pixels.2. 然后计算出一个scale的&#;,通过BitmapFactory.Options中的属性inSampleSize进行缩放。[html] view plaincopyBitmap bitmap; float imagew = ; // 目标大小 float imageh = ; // 目标大小 BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options(); bitmapFactoryOptions.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeFile(imageFile, bitmapFactoryOptions); int yRatio = (int)Math.ceil(bitmapFactoryOptions.outHeight/imageh); int xRatio = (int)Math.ceil(bitmapFactoryOptions.outWidth/imagew); if (yRatio > 1 || xRatio > 1){ if (yRatio > xRatio) { bitmapFactoryOptions.inSampleSize = yRatio; } else { bitmapFactoryOptions.inSampleSize = xRatio; } } else{ // inSampleSize = 1 } bitmapFactoryOptions.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(imageFile, bitmapFactoryOptions); myImageView.setImageBitmap(bitmap); Bitmap-recycle有用吗?以下是官方源代码的注释:Free the native object associated with this bitmap, and clearthe reference to the pixel data.This will not free the pixel data synchronously;it simply allows it to be garbagecollected if there are no other references. The bitmap is marked as "dead", meaning it willthrow an exception ifgetPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only becalled if you are sure there are no further uses for the bitmap.This is an advanced call,and normally need not be called, since the normal GC processwill free up this memory when there are no more references to this bitmap.(七)substring的恶梦[html] view plaincopypublic String substring(int beginIndex, int endIndex) { return new String(offset &#; beginIndex, endIndex - beginIndex, value); } String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count; } 也就是说即使你只想用其中的某几个字节,Java也自动把原始String的整个内容帮你保存下来了。[html] view plaincopyArrayList<String> oomStringList = new ArrayList<String>(); for (int i=0; i<*; i&#;&#;) { String srcString = new String(new char[]); String dstString = srcString.substring(0,1); oomStringList .add(dstString); } Reference:

android 获取网络速度 显示手机或者应用的实时网速,很多应用都有这块的内容,那么如何获取手机或者应用的实时网速呢?获取网速的原理是一段时间只能的数据总量除以

使用ListView实现界面的布局 spanstyle=font-family:Arial,Helvetica,sans-serif;background-color:rgb(,,);现在很多市面上的APP都用ListView来实现界面上的功能介绍或广告的植入,下面我们就来介绍

Android:仿QQ 发表说说/上传照片 弹出框 代码很简单,主要就是几个动画而已,图标什么的就随便找了几个,效果图:动画说明:1.点击右上角按钮,菜单从顶部下拉弹出,同时背景变暗;2.再次点击右上

标签: android内存泄露 工具

本文链接地址:https://www.jiuchutong.com/biancheng/376653.html 转载请保留说明!

上一篇:ffmpeg2.6.2在Ubuntu下使用NDK编译成.so文件(ffmpeg webui 0.9.6)

下一篇:android 获取网络速度(Android 获取网络时间)

  • 进项税转出会计分录如何做
  • 实际发放股票股利是什么意思
  • 公司成立之初做哪些
  • 财产租赁合同金额含税吗
  • 企业无票支出怎么入账
  • 营改增后还有企业所得税吗?
  • 资本公积什么时候做账
  • 暂估成本结转后怎么冲回
  • 净资产利润率等于净资产收益率吗
  • 企业购买原材料,货款未支付
  • 城市建设综合配套费征收管理办法
  • 境外增值税代扣代缴
  • 吊车租赁增值税税率最新2022
  • 增值税三个过渡期科目
  • 从事旅游的小型微利企业能享受哪些优惠?
  • 现金支票存根会计要签字吗
  • 适用增值税简易计税的项目
  • 罚没收入要交税吗
  • 代开普通发票要什么材料?
  • macos dock栏
  • 手动设定ip地址后连不上网
  • win10无法设置pin码怎么办
  • 公司开业前启动大会
  • 小型微利企业免税政策
  • 怎么取消网络手动连接
  • PHP:mb_ereg_search_setpos()的用法_mbstring函数
  • 买材料没发票怎么办
  • vue 页面生成pdf
  • 工伤个人承担的费用
  • 处置设备影响的净损益
  • 股东出资未注明投资款可以通过验资报告处理么
  • 个税申报中是否婚前各自首套贷款
  • 企业利润总额为负
  • 怎么把vue项目跑起来
  • 前期认证相符
  • 钱进公账怎么转账给别人
  • 职工探亲如何报差旅费
  • 塔吊租赁人工合同
  • 单位收的房租可以发工资吗
  • 固定资产折旧费计入成本吗
  • 个人所得税定额税率
  • 签发转账支票需要的单据
  • 跨年费用账务处理
  • 计入税金及附加的税种顺口溜
  • 减免残保金相关政策
  • 企业代扣代缴个税
  • 个人转租房需要注意什么
  • 以前年度多记成本费用税前扣除,今年要怎么做分录冲销
  • 包工包料工程如何报价合理
  • 公司账户没有钱怎么发工资
  • 收回理财款会计分录
  • 电费发票未到怎么入账
  • 税控盘减免税款需要结转吗
  • 明细账的登记方向与总账的登记方向是一致的
  • 库存商品账本填写样本
  • mysql格式化数值
  • mysql5.7解压版安装
  • 丢失的身份证补办后,原本的身份证还有用吗
  • 安装fedora33
  • 微软招聘流程
  • win8软件不兼容怎么办
  • linux解压操作
  • linux操作系统内核
  • Java反射机制和动态代理机制
  • 批处理执行bat文件
  • 什么叫屏蔽屏幕按键
  • node.js常用命令
  • nodejs搭建个人博客网站
  • python中的字符型
  • [置顶]津鱼.我爱你
  • three. js
  • android连接手机
  • 详解HTTPS 的原理和 NodeJS 的实现
  • New AssetBundle build system in Unity 5.0
  • jquery命名空间
  • 大连电子税务局app下载
  • 公允价值变动损益属于什么科目
  • 什么是联保发票呢
  • 厨房申购单怎么写
  • 经营碎石的税点多少
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设