简体   繁体   English

Android 原生库和内存消耗

[英]Android native libraries and memory consumption

I'm participating in development of an Android application that uses quite heavy native library (~20Mb).我正在参与一个 Android 应用程序的开发,该应用程序使用相当重的本机库 (~20Mb)。 Actual problem is that GC is working very frequently to resume some (but not too much) memory.实际问题是 GC 非常频繁地工作以恢复一些(但不是太多)内存。 Multiple times per second.每秒多次。

04-10 12:16:05.391  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6120K, 29% free 31708K/44359K, paused 20ms, total 20ms
04-10 12:16:05.501  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6121K, 29% free 31708K/44359K, paused 23ms, total 23ms
04-10 12:16:05.601  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6120K, 29% free 31708K/44359K, paused 23ms, total 23ms
04-10 12:16:05.702  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6120K, 29% free 31708K/44359K, paused 25ms, total 25ms
04-10 12:16:05.802  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6120K, 29% free 31708K/44359K, paused 28ms, total 28ms
04-10 12:16:05.902  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6120K, 29% free 31708K/44359K, paused 28ms, total 28ms
04-10 12:16:06.002  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6120K, 29% free 31708K/44359K, paused 26ms, total 26ms
04-10 12:16:06.122  18940-18951/com.some.demo D/dalvikvm﹕ GC_FOR_ALLOC freed 6120K, 29% free 31708K/44359K, paused 41ms, total 41ms

In Android Device Monitor (Heap tab) tool I can see that >25 MB is allocated by 1-byte array (byte[], boolean[]) , other parts consuming much lower heap.在 Android 设备监视器(堆选项卡)工具中,我可以看到 >25 MB 是由1 字节数组 (byte[], boolean[])分配的,其他部分消耗的堆要低得多。 In Allocation Tracker I can see a lot of lines with 3133456 byte[] for dalvik.system.NativeStart .在 Allocation Tracker 中,我可以看到dalvik.system.NativeStart有很多带有3133456 byte[]

It raises a question: does Android load all native library into memory or am I doing something wrong?它提出了一个问题:Android 是将所有本机库加载到内存中还是我做错了什么? I need some good explanation how Android deals with native libraries.我需要一些很好的解释 Android 如何处理本机库。

If there is no really effective way to reduce heap space taken by native library (removing dependency isn't an option, because not only Android uses it), then is there any other way to lower frequency of GC?如果没有真正有效的方法来减少本地库占用的堆空间(删除依赖不是一种选择,因为不仅Android使用它),那么还有其他方法可以降低GC频率吗?

EDIT: Adding images to provide more information.编辑:添加图像以提供更多信息。

Heap space:堆空间:堆

Memory allocation:内存分配:分配

I'm posting references that can be useful for those who will experience the same problems and lack of information on topic.我发布的参考资料对那些遇到同样问题和缺乏主题信息的人有用。

I've found an answer here https://stackoverflow.com/a/11312145/955107 that states: A GC_FOR_ALLOC is by itself not a sign of a problem in your application however:我在这里找到了一个答案https://stackoverflow.com/a/11312145/955107指出: GC_FOR_ALLOC本身并不是您的应用程序中存在问题的迹象:但是:

  • Android applications start with a small heap which grows (up to a point) when applications require more and more memory, and a GC_FOR_ALLOC is done before increasing the size of the heap. Android 应用程序从一个小堆开始,当应用程序需要越来越多的内存时,它会增长(达到一定程度),并且在增加堆的大小之前完成了 GC_FOR_ALLOC。 In this case GC_FOR_ALLOC is perfectly normal.在这种情况下 GC_FOR_ALLOC 是完全正常的。
  • If you allocate memory faster than the concurrent GC has time to free it up, GC_FOR_ALLOC is inevitable.如果您分配内存的速度比并发 GC 释放它的时间快,则 GC_FOR_ALLOC 是不可避免的。 And there's nothing inherently wrong with allocating memory faster than the concurrent GC can free up memory.并且以比并发 GC 释放内存更快的速度分配内存并没有本质上的错误。

Also a very useful source of information is video by Patrick Dubroy.还有一个非常有用的信息来源是 Patrick Dubroy 的视频。 It gave me more understanding what is happening in my application and what I need to analyze in my code: Google I/O 2011: Memory management for Android Apps .它让我更了解我的应用程序中发生了什么以及我需要在我的代码中分析什么: Google I/O 2011: Memory management for Android Apps Special attention to questions and answers on 53 minute 5 seconds and 55 minute 42 seconds.特别注意53分5秒和55分42秒的问答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM