[转]技巧和设计模式 --- 内存分配
1.不要创建 Java 对象
?
在性能敏感的代码里, 尽量避免创建 Java 对象,例如:
?
[1] 测量?onMeasure()
?
[2] 布局?onLayout()
?
[3]?绘图?dispatchDraw(), onDraw()
?
[4] 事件处理 ?dispatchTouchEvent(), onTouchEvent()
?
[5]?Adapter getView(), bindView()
?
2.GC, 垃圾回收
?
?[1] 整个程序暂停
?
?[2]?慢(大约几百个毫秒)
?
3.强行限制 (适用于调试模式)
?
?
private final HashMap<String, SoftReference<T>> mCache; public void put(String key, T value) { mCache.put(key, new SoftReference<T>(value)); } public T get(String key, ValueBuilder builder) { T value = null; SoftReferece<T> reference = mCache.get(key); if (reference != null) { value = reference.get(); } if (value == null) { value = builder.build(key); mCache.put(key, new SoftReference<T>(value)); } return value;}??
?