`

Managing Your App's Memory

 
阅读更多

避免内存泄露:将对方放入到全局变量

适时释放内存:比如生命周期回调函数

 

Android如何管理内存?

1.Anroid不用swap memory,用paging和memory-mapping

所有被修改过的内存都必须常驻内存,且不能被paged out.所以要适时释放所有引用的内存。

但是没有修改过的文件,可以被page out of RAM

 

2.共享内存

Android尝试通过如下方式在进程间共享RAM

1)所有app进程从Zygote Fork 出来。Zygote在系统启动时启动并加载框架代码和资源.

这样可以在各应用进程间共享框架的代码和资源

2) 多数的static data被mmapped到process.这样可以在进程间共享,需要时也可以paged out.

static data包括:Dalvik code(放在.odex文件中并直接mmapping),app资源和传统项目元素(比如*.so)

3)进程间的动态内存(用ashmem or gralloc显示分配共享内存)

 

3.分配和回收内存

1)每个进程的Dalvik heap被限定在单一地虚拟内存范围。该值定义logical heap size,可以按照需要增加,

但是不会超出系统给每个进程定义的限制值。

2)heap的logical size和heap占用的实际物理内存不一样。统计app heap时,Android计算PSS,会将进程间共享

的dirty和clean pages均分。PSS的总和可以作为物理内存的footprint.

3)Dalvik heap不影响logical heap的大小,也就是说Android不会整理Dalvik heap。

只有当logical heap的末尾有未用的空间时,Android会收缩logical heap size。

单不代表heap占用的物理内存不会被回收。在垃圾回收过后,Dalvik从heap查找未用的pages返回给内核。

所以,配对使用分配和释放大量内存可以引起几乎所有使用过的物理内存被回收。但是小内存却没有这个作用,

因为同一内存page可能被其它被引用的内存对象共享。

 

4.限制APP memory

Android给每个APP设置了一个heap size的极限值,当APP达到极限值后会出现outofmemoryError.

可以通过getMemoryClass()来确定还有多少heap空间可以申请。

 

5.APP如何管理内存?

app切换时,old app仍然被缓存在内存中。可以通过如下方式是内存使用更高效:

1)保守地使用Services

如果需要运行service完成后台工作,不要一直运行服务除非显示地完成一个任务。

同时服务完成工作时,一定要确保服务被停掉。

当启动一个服务时,系统将保持相应进程一直运行,这样这个服务就会一直占据内存。也就相应

减少系统缓冲应用的数目,让app切换可能更慢。在系统内存非常紧张以至于无法运行所有running

的服务时,系统将崩溃。最好的方法就是使用IntentService启动服务,完成任务后自动退出。

让一个不需要的服务一直运行,是最严重的内存管理错误。

2)在界面隐藏时释放内存

3)在收到内存紧张的消息时释放内存

4)避免用bitmaps浪费内存

5)使用优化过的数据容器

6)注意内存过热

7)注意使用抽象

8)注意使用外部库

9)使用proguard工具优化代码

10)使用zipalign优化apk

11)分析RAM使用

12)将app的模块分为多个进程。

 

 

 

 

扩展阅读:

Managing Bitmap Memory.

Running in a Background Service .

Best Practices for Performance.

Investigating Your RAM Usage.

 

 

 

 

 

 

分享到:
评论

相关推荐

    Avanguard-master_library_Anti-Intrusion_win32_

    Threads and modules callbacks* Handles keeper - prevents managing your app from other processes due to close handles of your process in external apps (for example CheatEngine or another memory editors...

    Pro Core Data for iOS, Second Edition

    The advanced portions of the book begin by showing you how to tune your apps' performance and memory usage, to give you a truly professional edge. You'll see how to version and migrate your data as ...

    IOS5 Programming Cookbook

    - **Build Settings**: Understanding build settings is important for optimizing your app's performance and ensuring compatibility across different devices. Key settings include architecture, ...

    微软内部资料-SQL性能优化2

    Application memory tuning provides more of the computer's virtual memory to applications by providing less virtual memory to the operating system. Although a system having less than 2 GB of physical ...

    Sams.Teach.Yourself.Big.Data.Analytics.with.Microsoft.HDInsight

    Using Apache Spark's in-memory computation model to run big data analytics up to 100 times faster than Hadoop MapReduce Perform real-time Stream Analytics on high-velocity big data streams with Storm ...

    Objective-C for Absolute Beginners iPhone, iPad, and Mac Programming Made Easy

    These frameworks simplify many common programming tasks, such as managing memory, handling user input, and rendering graphics. By leveraging Cocoa and Cocoa Touch, developers can create sophisticated...

    JavaScript Applications with Node.js, React, React Native and MongoDB

    Chapter 5: Managing Availability and Performance 5.1Indexing 5.2Availability through Replication 5.3 Sharding Chapter 6: NewsWatcher App Development 6.1 Create the Database and Collection 6.2 Data ...

    Msdn Magazine Feb. 2012

    - **Marketing Strategies**: Suggestions for marketing your app effectively to reach a wider audience. - **Compliance and Legal Considerations**: Overview of compliance requirements and legal ...

    iPhone Games Projects

    - **Mike Lee**: Creator of Tap Tap Revolution, one of the most downloaded games in the App Store's history. - **Mike Kasprzak**: His game Smiles was a finalist in the IGF 2009 Best Mobile Game ...

    apktool documentation

    It's not required to maintain your own aapt install under $PATH. (However, features like -a / --aapt are still used and can override the internal aapt) The addition of aapt replaces the need for ...

    Microsoft Visual C# 2013 Step by Step,最新资料

    Your hands-on guide to Visual C# fundamentals Expand your expertise—and teach yourself the fundamentals of Microsoft Visual C# 2013. If you have previous programming experience but are new to Visual ...

    Visual C++ 编程资源大全(英文源码 其它)

    An app to use with your AutoRun enabled CD's(413KB)<END><br>53,ezview.zip A shareware image viewer application(36KB)<END><br>54,LogTrace.zip A Class for Creating a Trace Log(153KB)<END><br>55,...

Global site tag (gtag.js) - Google Analytics