`
leonzhx
  • 浏览: 794188 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

1.  An obsolete reference is simply a reference that will never be dereferenced again.

 

2.  Memory leaks in garbage-collected languages (more properly known as unintentional object retentions) are insidious.

 

3.  If an object reference is unintentionally retained, not only is that object excluded from garbage collection, but so too are any objects referenced by that object, and so on.

 

4.  null out references once they become obsolete.

 

5.  Nulling out object references should be the exception rather than the norm. The best way to eliminate an obsolete reference is to let the variable that contained the reference fall out of scope. This occurs naturally if you define each variable in the narrowest possible scope.

 

6.  Whenever a class manages its own memory (i.e. object array), the programmer should be alert for memory leaks. Another common source of memory leaks is caches.

 

7.  If you’re lucky enough to implement a cache for which an entry is relevant exactly so long as there are references to its key outside of the cache, represent the cache as a WeakHashMap; entries will be removed automatically after they become obsolete. Remember that WeakHashMap is useful only if the desired lifetime of cache entries is determined by external references to the key, not the value.

 

8.  Under these circumstances, the cache should occasionally be cleansed of entries that have fallen into disuse. This can be done by a background thread (perhaps a Timer or ScheduledThreadPoolExecutor) or as a side effect of adding new entries to the cache. The LinkedHashMap class facilitates the latter approach with its removeEldestEntry method.

 

9.  A third common source of memory leaks is listeners and other callbacks. If you implement an API where clients register callbacks but don’t deregister them explicitly, they will accumulate unless you take some action. The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing them only as keys in a WeakHashMap.

 

10.  For more sophisticated caches, you may need to use java.lang.ref directly.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 7: Eliminate obsolete object references Item 8: Avoid finalizers and cleaners Item 9: Prefer try-with-resources to try-finally 3 Methods Common to All Objects Item 10: Obey the general contract ...

    Arcgis消除零碎图斑

    3. **使用Eliminate工具**:双击ArcToolbox中的Eliminate工具,打开Eliminate工具对话框。在对话框中,需要指定输入要素和输出要素及其路径和名称。此外,还需要选中“Eliminating polygon by border”选项。具体...

    rocksteady-sosp17-slides.pdf

    Rocksteady Overview: Eliminate Sync Replication Problem: Synchronous replication bottleneck at target Solution: Safely defer replication until after migration

    effective c++

    6. **Item 47: Use templates to eliminate redundant code** - **核心观点**:利用模板来消除重复代码,实现代码的重用。 - **示例应用**:展示如何通过模板类或模板函数来处理相似类型的多种数据类型,提高代码...

    FairyGUI-Unity-Plugin-3.4.0.zip

    - NEW: Support for setting selectedIcon of list item in editor. - IMPROVED: Add UIConfig.depthSupportForPaitingMode. - IMPROVED: Set sorting order of popup automatically. - FIXED: Fixed a text layout ...

    Python库 | eliminate-newlines-1.2.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:eliminate-newlines-1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    JavaScript Object Programming(Apress,2015)

    This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...

    effectice_java第二版 英文

    6. **条目6:消除过期的对象引用(Eliminate Unused Object References)** 过期的对象引用可能导致内存泄漏,确保及时释放不再使用的对象。 7. **条目7:覆盖equals时总要覆盖hashCode(Override equals() and ...

    acpi控制笔记本风扇转速

    Debug Version: 154.6K Code, 63.0K Data, 217.6K Total Current Release: Non-Debug Version: 78.1K Code, 17.1K Data, 95.2K Total Debug Version: 155.4K Code, 63.1K Data, 218.5K Total 2) iASL Compiler/...

    ComponentOne IntelliSpell2012 v3

    Deliver Professional Applications: Eliminate embarrassing typos from your applications (Windows and Web Forms, UI controls, ToolTips, message boxes, and more). Improve the Quality of Your ...

    英语:必修5人教版同步练习及答案(53页).docx

    - 需要在农业上投资四十多亿美元以消除世界饥饿:eliminate(消除) 这些知识点涉及到英语语法、词汇、句子结构以及日常交流中常见的表达方式。通过这些练习,学习者可以提升词汇量、熟悉语境中的单词用法,同时...

    x-eliminate:简单的十字消除游戏

    《x-eliminate: 简单的十字消除游戏》 在数字娱乐领域,游戏开发是一项充满创意和技术挑战的工作。本文将围绕“x-eliminate”这款游戏,探讨它所涉及的技术点,包括游戏机制、图形渲染以及编程语言的运用。 “x-...

    Eliminate(消除).rar

    在"Eliminate"游戏中,玩家可能会面临各种挑战,例如时间限制、步数限制、特殊元素(如炸弹、冰块)等,这些都增加了游戏的趣味性和挑战性。游戏的关卡设计也是关键,通常会随着进度逐步增加难度,引入新的元素和...

    ComponentOne IntelliSpell 2013 v1

    Deliver Professional Applications: Eliminate embarrassing typos from your applications (Windows and Web Forms, UI controls, ToolTips, message boxes, and more). Improve the Quality of Your ...

    Java Crash Course [2016]

    Chapter 6: User Input: Getting Data in Runtime Chapter 7: The String Object Chapter 8: Boolean Logic Chapter 9: Loops and Arrays Chapter 10: Methods Chapter 11: Inheritance and Polymorphism Chapter 12...

    工业工程在超市中的应用提升服务效率.pdf

    - **ECRS四原则**:Eliminate(消除)、Combine(合并)、Reorder(重排)、Simplify(简化),用于流程改进。 - **流程程序分析**:识别并改进工作流程,减少浪费和提高效率。 - **布置和经路分析**:优化空间...

    StarCraft2-Happy-Kitting:带有DDPG的StarCraft2 AI

    《星际争霸2》 ...Victory Condition : Eliminate the enemy Time Limit : 1 min 30 sec Modified from PySC2 mini games ( DefeatRoaches ) 运行: ~\DQN python3 main.py ,不带任何命令行参数 总体表现令

    iOS9 Programming Fundamentals with Swift(Swift.Xcode.and.Cocoa.Basics)

    This partial object-orientation introduces complexities and inconsistencies that Swift aims to eliminate. #### Transitioning from Objective-C to Swift Many developers, including Matt Neuburg, ...

Global site tag (gtag.js) - Google Analytics