`
ycljf86
  • 浏览: 74342 次
  • 性别: 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
分享到:
评论

相关推荐

    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 ...

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

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

    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 ...

    S7A驱动720版本

    - Passing of invalid OPC Item IDs caused a memory leak of the driver's global memory. After the global memory was exhausted, the driver crashed. - Writing of string values failed when using ...

    Bochs - The cross platform IA-32 (x86) emulator

    - Implemented Process Context ID (PCID) feature - Implemented FS/GS BASE access instructions support (according to document from http://software.intel.com/en-us/avx/) - Rewritten from scratch SMC ...

    Borland Delphi 2005 Architect Update 3

    named assembly metadata4.00 of 5 Closed9021 Memory Leak in TreeView0.00 of 5 Closed8866 Assigning long string values (greater than 8192 characters) to TStringFields results in access violation.5.00 of...

    Senfore_DragDrop_v4.1

    Drag and Drop Component Suite Version 4.1 Field test 5, released 16-dec-2001 ?1997-2001 Angus Johnson & Anders Melander ... ------------------------------------------- Table of Contents: ...

    Delphi7.1 Update

    Delphi 7.1 Update Release Notes=======================================================This file contains important supplemental and late-breakinginformation that may not appear in the main ...

    端口查看工具

    default, because it seems that reading the DNS cache causes a memory leak on some Windows 7/x64 systems. * Version 1.96: o Fixed bug: CurrPorts randomly failed to display remote port numbers of ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX Memory leak in TdmMain.dmMainDestroy method. Version 1.5 ----------- - FIX: In the methods TFlexPanel CombineSelected and BreakApartSelected - fixed saving of Undo/Redo actions. - FIX: In ...

    VclZip pro v3.10.1

    - Improved memory performance when working with archives containing extremely high number of compressed files. ==================== Version 3.06 Build 2 Made Delphi 2005 compatible Other assorted ...

    php.ini-development

    application users can inadvertently leak otherwise secure information. ; This is php.ini-development INI file. ;;;;;;;;;;;;;;;;;;; ; Quick Reference ; ;;;;;;;;;;;;;;;;;;; ; The following are all ...

    hls.min.js

    throw d.context=e,d}if(r=this._events[t],o(r))return!1;if(i(r))switch(arguments.length){case 1:r.call(this);break;case 2:r.call(this,arguments[1]);break;case 3:r.call(this,arguments[1],arguments[2]);...

Global site tag (gtag.js) - Google Analytics