您的位置:首页 > 移动开发 > Android开发

android核心技术之性能分析工具Memory Monitor

2016-12-10 12:16 357 查看

Memory Monitor

Android Monitor provides a Memory Monitor so you can more easily monitor app performance and memory usage to find deallocated objects, locate memory leaks, and track the amount of memory the connected device is using.

Memory Monitor 可以方便我们非常简单的监视应用的性能,内存的释放,定位内存泄漏和跟踪内存的实时使用情况。

The Memory Monitor reports how your app allocates memory and helps you to visualize the memory your app uses. It lets you:

Show a graph of available and allocated Java memory over

time.(实时显示已使用和可使用的内存的示意图)

Show garbage collection (GC) events over time.(实时显示GC事件的时间)

Initiate garbage collection events.(初始化GC事件)

Quickly test whether app slowness might be related to excessive

garbage collection events.(快速测试应用缓慢是不是和过多的GC事件有关)

如果程序出现大量的内存分配和释放,就意味着产生了频繁的GC。当GC线程运行时,其他线程会停止工作(包括UI线程),直到GC完成。所以频繁的GC,必然会导致UI线程的卡顿。特别是性能比较差的手机上,尤为明显。

Quickly test whether app crashes may be related to running out of

memory.(快速测试应用崩溃是不是和OOM内存泄漏有关)

操作

如下图:

1.在Android Monitor界面中选择Monitors—Memory—选择机器—选择应用—再点击如图所示的图标,开始监视内存的使用情况,再点击图标,停止监视内存的使用情况。



生成的alloc文件保存在captures目录下,如下图:



分析alloc文件:

先点击Size,将Size按从大到小的顺序排列,我们可以看到Thread 14占用了96.58%的Size,明显我们要看看此线程的内存使用情况,我们一步步点开此线程,我们可以看到占用内存的方法是getMD5的第134,136行:



我们右键弹出:Jump to Source,点击Jump to Source,直接跳转到对应的代码中。



此样例,我是使用android核心技术之性能分析工具TraceView的例子,跳转过去,我们可以看到是如下的代码:

MessageDigest md = MessageDigest.getInstance("MD5");
result = new BigInteger(1, md.digest()).toString(16);


也就是计算MD5的方法和BigInteger转换为String这二个方法占用了大量的内存,需要优化。

可见,Memory Monitor是可以精确的定位是那行代码导致内存的问题,功能是非常的强大的。

参考资料:

1.Android性能优化——内存优化

http://blog.csdn.net/lylwo317/article/details/53151604

2.Memory Monitor

https://developer.android.com/studio/profile/am-memory.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息