前端时间看见java四种引用的介绍,
觉得软引用看起来可以用作内存优化.
然后看博客说,LRU内部是维护了一个有强引用的LinkedHashMap,他会根据算法将最靠前的资源从集合中移除.并没有使用到软引用.
所以请问一下,java的软引用或者弱引用在android中有应用场景吗?
LRU这样的做法,除了让缓存更大可能被复用到,还有其他优势吗?
20:56 增加
看到一篇博客说android2.3(api9)开始,内存回收器即使在内存充足的情况下,软引用和弱引用指向的对象依然有可能被回收.
那么这样的话利用弱引用来创建的Handler用于防止Handler内存泄露的方案是不是很不可靠?
//代码引用自博客
public class TestReferenceActivity extends BaseActivity {
static class MyHandler extends Handler {
private WeakReference<TestReferenceActivity> reference;
public MyHandler(Activity activity) {
//使用弱引用包裹当前的activity
reference = new WeakReference(activity);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_reference);
//解决Handler可能造成的内存泄露
//通过弱引用实现,如果当前activity需要被回收了,而且Handle持有的
//activity是被弱引用包装的,则垃圾回收器可以释放掉此activity。
MyHandler myHandler = new MyHandler(this);
}
}
Soft references have indeed been limited in their original functions after the 2.3 system, but they can still be used, but they are not as easily recycled during GC on Android as on the native JVM. For specific use, I recommend this article to you
Soft references and weak references are not unusable, but their behavior is unpredictable.
We cannot know exactly when weakly referenced resources will be recycled. In typical image caching situations, the LRU algorithm is more efficient than weak references and can effectively avoid multiple creation/recycling of resources.
Weak references are more suitable for certain situations where memory leaks are prone to occur. For example, in AsyncTask, we can use weak references to prevent memory leaks.
Soft references are actually very useful in actual Android development, such as asynchronous loading of images. The most typical one is that imageloder also uses soft references when dealing with cache optimization. In addition, soft references feel to me that they do not help in performance optimization. There are too many, mainly for good processing of oom. If there is a need for performance optimization, it is actually recommended to use weak references
After answering, I saw that there was still a question below the question. In fact, the reason why soft references are used to prevent the handler from OOM is due to the special mechanism of Android. When an Android main thread is created, there will be a Looper object at the same time. A MessageQueue will be implemented. When we create the handler object, whenever we use the handler to put a message into the MessageQueue, the message will hold a reference to the handler object. Therefore, when the Activity is ended and before the message is taken out, this The message always exists, and the message holds a reference to the handler. The handler is created in the activity and will hold a reference to the activity. Therefore, this activity cannot be recycled by gc, and OOM will occur. All non-static objects in Java will hold strong references to the current object, while static objects only hold weak references to the current class. This solves the problem that when the activity is ended, the message cannot be processed, causing the message to permanently hold the handler. Reference, the handler permanently holds the reference to the activity.