android-手机内存分配和回收机制研究

简介:

 Theory: There are two kinds of memory allocation mechanism :

 

(1)    Some large application ,like oracle client ,world of warcraft game :


( We can think about it like Buffet( 自助餐) if you paid, you can control what you eat and do not need to pay anymore )

 

 

This kind of application will have a minimum configuration of memory ( for example ,the WOW game will have at least 1gigabyte memory) ,this minimum memory is the largest estimation of the memory that it may use ,it will never ask the Operation System for extra memory when running it .(For example ,the wow.exe only need 400m memory ,the left memory are reserved for temp files ,swap files ,object files and so on ,so even it only needs 400M,the rest 600M are unavailable for other application .)

So this kind of application has only two status:  (Can run / cannot runOnce it runs ,it will always run and do not need to worry about the memory .

 

(2)    Small application:

Most of the normal application are acted like this way ,it applies for a small memory ,and when it runs and needs more memory ,it asked the Operation system for more memory.


(we can think it as “Choose dishes from menu”(点菜) ,if we needs more ,we order more ,pay more)

 

So this kind of application will have three status:

(cannot run /Can run/ can run but future the memory used out)

 

During running ,it needs more memory and applied from OS ,so maybe onetime ,the OS cannot supply enough memory, this application will shutdown.

 

 

 

 

Our issue:

 

Based on the above theory ,we can take a look at the problem ,why sometimes ,the application will shut down if we scroll the screen very frequently in search result page. Because android mobile application is using Mechanism 2

 

Problem: when we go to the search result page ,if we scroll down the screen very frequently ,the application thread will shut down because of the memory used out.

 

Phenomenon:

 scroll down very frequently  

->Dalvik VM used out exception


 

Reason:

The Heap memory can be divided into parts ,one is reserved for java objects ,the other is reserved for native C objects.(They cannot mix together)

If we scroll up /down the screen very frequently ,the new native objects are created and ONLY the native heap memory are recycled. And the recycling speed is much slower than the allocating memory speed ,that’s why the heap memory will used out.

From the log ,we can see ,that only native heap memory are recycled (GC_EXTERNAL_ALLOC stands for the native heap memory recycle)

And most the memory recycled are small( less than 150KB) ,that’s because android based on “slab” mechanism to allocate memory ,and the slab has a limit size.


 

But what the size of the native heap memory it allocated when a new native c object created? The answer is 320KB in our test


So allocate more and recycle less ,at last ,the memory will used out and the application process will die.

 

 

Solution;

When configure the emulator ,please set the RAM(SD card) to be a large number ,and restart the emulator.

Go to <ANDROID_ROOT>\.android\avd\<Your avd>\config.ini ,change the sdcard.size






本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/835923,如需转载请自行联系原作者

目录
相关文章
|
6月前
|
网络协议 Android开发 数据安全/隐私保护
Android手机上使用Socks5全局代理-教程+软件
Android手机上使用Socks5全局代理-教程+软件
5008 2
|
25天前
|
算法 Linux 调度
深入探索安卓系统的多任务处理机制
【10月更文挑战第21天】 本文旨在为读者提供一个关于Android系统多任务处理机制的全面解析。我们将从Android操作系统的核心架构出发,探讨其如何管理多个应用程序的同时运行,包括进程调度、内存管理和电量优化等方面。通过深入分析,本文揭示了Android在处理多任务时所面临的挑战以及它如何通过创新的解决方案来提高用户体验和设备性能。
39 1
|
1月前
|
存储 安全 Android开发
探索Android与iOS的隐私保护机制
在数字化时代,移动设备已成为我们生活的一部分,而隐私安全是用户最为关注的问题之一。本文将深入探讨Android和iOS两大主流操作系统在隐私保护方面的策略和实现方式,分析它们各自的优势和不足,以及如何更好地保护用户的隐私。
|
2月前
|
消息中间件 存储 Java
Android消息处理机制(Handler+Looper+Message+MessageQueue)
Android消息处理机制(Handler+Looper+Message+MessageQueue)
44 2
|
24天前
|
Linux Android开发 iOS开发
深入探索Android与iOS的多任务处理机制
在移动操作系统领域,Android和iOS各有千秋,尤其在多任务处理上展现出不同的设计理念和技术实现。本文将深入剖析两大平台在后台管理、资源分配及用户体验方面的策略差异,揭示它们如何平衡性能与电池寿命,为用户带来流畅而高效的操作体验。通过对比分析,我们不仅能够更好地理解各自系统的工作机制,还能为开发者优化应用提供参考。
|
2月前
|
消息中间件 存储 Java
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
|
2月前
|
消息中间件 存储 Java
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
58 1
|
2月前
|
存储 安全 数据安全/隐私保护
探索安卓与iOS的隐私保护机制####
【10月更文挑战第15天】 本文深入剖析了安卓和iOS两大操作系统在隐私保护方面的策略与技术实现,旨在揭示两者如何通过不同的技术手段来保障用户数据的安全与隐私。文章将逐一探讨各自的隐私控制功能、加密措施以及用户权限管理,为读者提供一个全面而深入的理解。 ####
73 1
|
2月前
|
消息中间件 存储 Java
Android消息处理机制(Handler+Looper+Message+MessageQueue)
Android消息处理机制(Handler+Looper+Message+MessageQueue)
55 2
|
3月前
|
存储 缓存 Android开发
Android RecyclerView 缓存机制深度解析与面试题
本文首发于公众号“AntDream”,详细解析了 `RecyclerView` 的缓存机制,包括多级缓存的原理与流程,并提供了常见面试题及答案。通过本文,你将深入了解 `RecyclerView` 的高性能秘诀,提升列表和网格的开发技能。
77 8