Effective Java 06 Eliminate obsolete object references
2014-02-28 22:58
351 查看
NOTE
Nulling out object references should be the exception rather than the norm.
Another common source of memory leaks is caches.
Once you put an object reference into a cache, it's easy to forget that it's there and leave it in the cache long after it becomes irrelevant. There are several solutions to this problem. 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. (LinkedHashMap can remove the oldest cache item by reomveEldestEntry. More is in java.lang.ref)
A third common source of memory leaks is listeners and other callbacks.
// Can you spot the "memory leak"?
public class Stack {
private Object[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public Stack() {
elements = new Object[DEFAULT_INITIAL_CAPACITY];
}
public void push(Object e) {
ensureCapacity();
elements[size++] = e;
}
public Object pop() {
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
/**
* Ensure space for at least one more element, roughly
* doubling the capacity each time the array needs to grow.
*/
private void ensureCapacity() {
if (elements.length == size)
elements = Arrays.copyOf(elements, 2 * size + 1);
}
}
Nulling out object references should be the exception rather than the norm.
Another common source of memory leaks is caches.
Once you put an object reference into a cache, it's easy to forget that it's there and leave it in the cache long after it becomes irrelevant. There are several solutions to this problem. 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. (LinkedHashMap can remove the oldest cache item by reomveEldestEntry. More is in java.lang.ref)
A third common source of memory leaks is listeners and other callbacks.
// Can you spot the "memory leak"?
public class Stack {
private Object[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public Stack() {
elements = new Object[DEFAULT_INITIAL_CAPACITY];
}
public void push(Object e) {
ensureCapacity();
elements[size++] = e;
}
public Object pop() {
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
/**
* Ensure space for at least one more element, roughly
* doubling the capacity each time the array needs to grow.
*/
private void ensureCapacity() {
if (elements.length == size)
elements = Arrays.copyOf(elements, 2 * size + 1);
}
}
相关文章推荐
- 一些常见的debug错误
- AttributeError: 'module' object has no attribute 'Frame' 解决方法
- error C2504: 'CObject' : base class undefined
- Matlab报错BLAS loading error: dlopen: cannot load any more object with static TLS 处理办法
- 【C#开发iOS】Objective-C与C# with Xamarin的开发代码对比
- objective -c OC语法学习小结
- Multiple markers at this line - The type java.lang.Object cannot be resolve
- object.constructor
- DisplayObject等显示对象基类
- objective c 学习(一)
- iOS开发:详解Objective-C runtime
- Object转Integer
- Effective C++ Item 17 Store newed objects in smart pointer in standalone statements
- Objective-c: 移除字符串中的指定字符
- 关联 objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects
- ios反射调用类的方法(objectiveC)
- Objective-C消息机制的原理
- Dictionary Object (Scripting Runtime Library)
- IOS,Object C学习过程中遇到的attributes
- core foundation与objective-c的区别