The Research on building cache for Android platform using soft references

时间:2022-10-26 07:18:22

Abstract:Along with the mobile platform increasing development,Android operating system has become the dominant operating system on mobile platforms.As the result of the limitation of Android system computing environment and network resources,it is especially important to apply Cache technology to improve the efficiency of data access.In this paper,a detail study is made on soft reference technology of mobile platform and soft reference strategy is proposed to build a cache on the Android platform application.It has increased the use of system memory efficiently and data access performance.Meanwhile soft reference object can help the Dalvik virtual machine to manage memory and eliminate potential memory leaks well.

Keyword:Android;Soft Reference;Cache

In the Android System Computing Environment, system memory and the bandwidth of the wireless communication is very limited.This requires that the Android client should try to reduce the commnunication overhead over the wireless and conservation memory resources when in use. Caching techniques, as the classical techniques of improving data access performance, have achieved successful application in the field of computer communication[1]. When introduced Caching techniques into the Android mobile computing environment,it is conducive not only to reduce the overhead of the Android client in the wireless communication procedure, but also to improve the hit rate of the valid data in the memory.

Soft references is a high level of Java objects. It is very suitable for creating the cache to store some expensive common resources in the Android client environment.The client can check the availability of the cached data before performing query operations[2].In general,data can be directly obtained in the repetitive operations.While data out of the range of cache management need to be loaded asynchronously over wireless network.

1 The characteristics of soft references

A soft reference is the object referenced by SoftReference with a relatively long life cycle.In the Android environment,only when system is starved of memory will it release the soft reference object.So we can use them to store some expensive resources which need to be loaded asynchronously,such as online picture data in which each soft reference object that has stored picture data existing in the entire application lifecycle is also a java object[3]. Besides,when the system is out of memory,the content cached by soft reference can be released and recycled by Dalvik virtual machine.

Dalvik virtual machine often perform garbage collection operations according to Mark-Sweep algorithm, which causes the uncertainty of running time. However,provided that the a object which reference exists will be in the memory all the time.If more and more objects like this exist,which even beyond the memory capacity of Dalvik virtual machine,the system will throw OOM(Out of Memory) error.It is more flexible to control the lifecycle of less important object in the application through soft references.When the system is out of memory,these objects can be recycled temporarily.If the system memory is also lacking after the Dalvik virtual machine has finished all the recycling project,the Dalvik will throw OOM error.

2 The management of soft references

We can flexibly control the liftcycle of Java object in the application with soft references.The method of using it is shown in Java code below:

MyObject ref=new MyObject();//Create an object of type MyObject

SoftReference softRef=new SoftReference(ref);//Create a soft reference of ref object

ref=null;//Eliminate the strong reference to the MyObject object

MyObject anthorRef=(MyObject)softRef.get();

//Obtain the object through soft reference

After executing the above code,there are two ways to reference the MyObject object.One is for the soft reference of softRef and the other one is for the strong reference of ref means MyObject object is a strongly reachable object.When the ref is set to null,the garbage collection thread in the Dalvik will make a distinction between softly reachable object and other strongly reachable object in the system. What’s more,before the Dalvik throws OOM,the garbage collection thread would recycle the unused softly reachable object as preferentially as possible. And the Dalvik will reserve the newly created softly reachable object as much as possible.Before recycling the MyObject object,the get() method of softRef obtains the strongly reference of MyObject object. And after recycling,the get() method returns null[4].

Besides the particularity of soft reference object,SoftReference object has the generality of Java object.When the softly reachable object reclaimed, this SoftReference object is already not worthy to exist although the get() method of SoftReference object returns null.That is because the soft reference object it keeps has alreay not existed. At this time,an appropriate clearance mechanism is needed to avoid the “new” memory leaks caused by a large number of SoftReference objects.The Java.long.ref package provides ReferenceQueue object.The methods of using it is shown in Java code below:

ReferenceQueue refQueue=new ReferenceQueue();//Create a reference queue

SoftReference softRef=new SoftReference(ref,refQueue);

//Add the soft reference into the reference queue

while(null !=(refQueue.poll())){

// clear ref object

}

After executing the above code,with the soft reachable object stored by this SoftReference object being recycled by Dalvik garbage collection thread,SoftRefrence object referenced by softRef is added to ReferenceQueue[5].ReferenceQueue is a queue, which stores SoftReference objects that lost reference object.This queue has the characteristic of “FIFO”.We can call the poll() method to check whether the soft reference objects it concerns is recycled or not. If the queue is empty, it returns null,otherwise it returns a SoftReference object at the head of the queue. Meanwhile we can check which SoftReference has been recycled and then clean up the SoftReference objects which lost reference object.

3 The implementation strategy of soft reference cache in Android platform application

In the Android application of C/S structure,the client usually needs to obtain pictures and other data information from the server.However,loading pictures is a time consuming and traffic consuming process.If the issue is not properly handled,it will make users feel stuck and the application is not friendly.There may be a lot of repetitive operations when loading pictures. From this angle embarking, there is no need to refetch the network pictures from the same URL address.We can keep some expensive image resources in memory and use LRU algorithm to optimize storage.Besides,the other image resources can be saved in the SDCard of client.As the pictures take up only a little space in the SDCard,it not only ensures the speed of loading pictures but also saves the traffic of the wireless communication. This caching strategy applys to the asynchronous image loading by ListView.Moreover,It can futher optimize memory allocation and better avoid throwing ANR(Application Not Responsed) exception information by the application,which is good for the interaction with users.The specific implementation can be divided into several steps below:

(1)Create buffer cache in memory.We can use LinkedHashMap to create strongly reference cache used to store URLs of network pictures and corresponding Bitmap objects.Because LinkedHashMap store data in the bottom,it has a higher efficiency to access data. ConcurrentHashMap is used to store URLs of pictures and corresponding Bitmap soft reference objects,which has a guarantee for synchronizing data and accessing safely to threads.

(2)The implementation of load pictures asynchronously.Android provides AsyncTask class which encapsulate basic models used in asynchronous operations.We can execute pictures request operation in doInBackground() method.After loading,the AsyncTask’s built-in handler send message to main thread and all the UI operation achieved in onPostExecute() method.

(3)Design callback and execute LRU replacement algorithm.Two methods is needed to included in designed callback interface.One is for pass URLs of pictures and loaded Bitmap objects to UI thread,the other one is for replace inactive picture resource in strongly reference cache to soft reference cache through executing LRU algorithm.

We can achieve ListView loading network pictures asynchronously with cache,those concrete procedures are as shown below.

4 Conclusion

Soft references, which have strong reference function,can be used in the Android computing environment with limited system memory and wireless broadband to cache some expensive resources that need to be loaded asynchronously.It can improve the utilization of memory and the performance of accessing data.This paper focuses on the characteristics and management methods of soft references.For the Android platform application, the implementation strategy of soft reference in loading pictures asynchronously by ListView is proposed. There is still one thing should be concerned that although soft reference can control the life cycle of Java object in the application flexibly,it may not effective to android client application which is very dependent on cache in performance.Furthermore,it cannot replace complex caching framework that can provide flexible termination period.

[References]

[1]张昊,潘祖烈.Java软引用防止内存泄漏技术的研究[J].安徽教育学院学报,2006,24(6):42-45.

[2]杨安宁,周莹,吕康,等.一种Android平台GIS软件的新型数据缓存策略的实现[J].计算机与现代化,2012,(10):46-49.

[3]焦忭忭.移动数据库在管理信息系统中的应用研究[D].武汉理工大学,2006.

[4]曾丽娟,娄松涛.移动客户机中缓存技术的应用研究[J].电脑知识与技术(学术交流),2007,3(18):1657-1658.

[5]周毅敏,陈榕.Dalvik虚拟机进程模型分析[J].计算机技术与发展,2010,20(2):83-86.

上一篇:RFID匹配数据的有效性分析和统计检验方法 下一篇:IPv6报文格式浅析