`
ycljf86
  • 浏览: 76769 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

解释context memory leak的文章

 
阅读更多
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html

The "static" keyword has different meanings when it comes to "static variables" vs. "static classes" in Java.

A static variable is a variable that belongs to all instances of a particular class. It will not be reclaimed by the GC when a particular instance of the class is garbage collected... it will stay in memory until you explicitly set it to null. The reason why it is bad to hold a static reference to a Drawable is because a Drawable usually holds a reference to a View and that View usually holds a reference to its parent Activity. As a result, the static Drawable will force the Activity to stay in memory even after the Activity is destroyed (unless you explicitly set the static Drawable to null, of course).

Static classes in Java don't really have the same meaning as static variables in Java. A static class declaration gives you a way to declare an inner class as if it was declared in a separate .java file... and that's pretty much it. A non-static inner class on the other hand is implicitly associated with its outer class... unlike static inner classes, an instance of a non-static inner class cannot exist without an instance of its outer class as well.

To answer your second question, declaring the mHandler as static would not have the same effect as the above sample code. Making the handler static means that it would be shared by all instances of the Activity (i.e. if you rotated the screen causing the Activity to be destroyed, the same Handler object would be used by the newly created Activity instance as well). In the sample code above, the Handler is declared as non-static which means that a new Handler will be created for each new Activity that is instantiated.

Yes, the Runnable is static so a single instance will be allocated and will be shared across all Activity instances (a new one will not be created for each new Activity that is created). I explain why it is important for the Runnable to be static in the second to last paragraph of the post.


java static and non-static class and variable
https://drive.google.com/file/d/0B-5-QeQdT3FxQUdnUWNFTUMxdXc/view
分享到:
评论

相关推荐

    Android_memory-leak-debugging.pdf.zip_Android memory le_android_

    本资源"Android_memory-leak-debugging.pdf.zip"聚焦于Android平台上的内存泄漏调试,这对于从事Android游戏开发的专业人士来说尤其重要。 内存泄漏是指程序在申请内存后,无法释放已申请的内存空间,一次小的内存...

    Notepad++ v5.8.4

    Fix memory leak problem while switching tab. Fix User Defined Language dialog docking problem under Windows 7. Fix backwards search with Asian codepage problem. Add a new capacity in context menu: the...

    Proxool-0.9.1

    To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 十二月 02, 2013 8:19:43 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc SEVERE: The web application [/...

    vld(Visual Leak Detector 内存泄露检测工具 源码)

     Visual Leak Detector detected 1 memory leak.  第二行表示57号块有4字节的内存泄漏,地址为0x003A89C0,根据程序控制台的输出,可以知道,该地址为指针p。程序的第7行,f()函数里,在该地址处分配了4字节的堆...

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

    A process can leak resources such as process memory, pool memory, user and GDI objects, handles, threads, and so on. Memory Concepts (X86 Address Space) Per Process Address Space Every process ...

    使用Android Studio检测内存泄露(LeakCanary)

    1. 避免静态变量长时间持有Activity或其他Context相关的对象,因为这可能导致Context泄露。 2. 使用WeakReference或SoftReference来弱化对象之间的引用,使得它们能够在不再被引用时被垃圾收集器清理。 3. 注意...

    Android的内存机制和溢出说明

    由于Android设备的内存有限,理解内存机制有助于优化应用性能,防止内存溢出(Memory Leak)等问题。 首先,Android应用的内存限制相对较低,尤其是Dalvik虚拟机,最大堆大小通常为16MB。这要求开发者谨慎管理内存...

    HandleMemoryLeakDemo.zip

    - 使用Application Context:若Handler需要在多个组件之间共享,考虑使用Application Context而不是Activity Context,以减少内存泄漏的风险。 6. **检测和定位内存泄漏**: Android Studio提供了Memory Profiler...

    博客 工作中遇到的Android内存优化问题demo

    2. 内存泄漏(Memory Leak): 当一个对象不再使用但仍然被引用,垃圾回收器无法释放其占用的内存,就可能导致内存泄漏。检查内存泄漏的方法包括使用Android Studio的内存分析工具,如Allocation Tracker和Heap ...

    JAVA性能瓶颈和漏洞检测].JProbe.Suite.v7.0.part1

    智能化内存分析:通过Leak Doctor发现可能的内存泄露源; Aggregate Memory Footprint:理解对象创建的实际开销; Reference Graph 和 Instance Detail:跟踪内存使用和对象引用; 垃圾回收分析:检测过多的短期...

    Android堆Dump文件分析,测试Demo

    - 长期保持静态变量引用导致的内存泄漏:比如在一个Activity中创建静态变量持有Context,当Activity被销毁,静态变量仍然存在,阻止Context被回收。 - 单例模式中的资源未正确释放:如果单例中持有大量资源,如...

    Android的MemoryLeakIn

    在Android开发中,内存泄漏(Memory Leak)是一个严重的问题,它会导致应用性能下降,甚至可能导致应用无响应(ANR)。本话题将深入探讨Android中的内存泄漏及其解决策略,结合"MemoryLeakInAndroid-master"这个项目...

    Android编程内存溢出与防范方法浅析

    在Android编程中,内存溢出(Memory Leak)是一个常见的问题,尤其对于有限的移动设备资源而言,内存管理显得尤为重要。Android的Dalvik虚拟机虽然在内存管理上与Java虚拟机有相似之处,但由于其内存限制(通常是16...

    Android 内存泄漏调试经验分享

    - **Leak Suspects Report:** 查找疑似泄漏的对象。 - **Dominator Tree:** 查看哪些对象占用了最多的内存空间。 - **Memory Regions:** 分析内存区域以找出大对象的分布。 综上所述,了解并掌握这些常见的...

    Tomcat报错: JDBC unregister 解决办法

    To prevent a memory leak, the JDBC Driver has been forcibly unregistered. ``` 这里,`[web application]`是你的应用名称,`[net.sourceforge.jtds.jdbc.Driver]`是具体的JDBC驱动类名。 解决这个问题有以下...

    Visual C++ 编程资源大全(英文源码 图形)

    and not the picture itself(14KB)<END><br>96,bitmapdc.zip A handy class that provides a memory bitmap device context(23KB)<END><br>97,ccolor.zip A class that provides simple color manipulation ...

    ActiveState Komodo IDE 10.2.1.89853 Setup + Keygen

    Additionally we've fixed a long standing memory leak which caused users to have to restart Komodo every few days, that should no longer be the case. - Other Mentionables - Improved source code ...

Global site tag (gtag.js) - Google Analytics