0

Hi Developers Of Android,

I am trying to figure out a way to implement a in-memory cache; but its size will be relatively big by comparison with the dvm heap size. I had planned to use nio and/or Parcel class to serialize my objects then store them on the native memory; however i cannot predict the performance of reconstructing those objects from their bytes indeed. Furthermore, i couldn't have found anything useful which teaches how to use Parcel and Parcelable to do some custom serialization. I know it is for IPC in android; and i didn't find inspecting the Parcel.cpp to understand the internals of the Binder protocol so fascinating and feasible. On the other hand, I cannot make use of LRUCache class either due to the dvm heap size limit which means that i will not be able to store my java objects on the dvm heap as they are. According to the researches on the web i've made so far, the only way to exceed the dvm heap size is to tell my clients to root their devices before they install my application(or i misunderstood. If that is the case, please do correct me.) so they can modify the heap size option of their devices.

I am trying to avoid NDK to implement the cache for now, since i have to pass my c/c++ instances to the java side eventually.

Long story short, Would any developer mind telling me the best way to implement an in-memory cache which may penetrate dvm heap size limit and run fast as well? Or At least, if you can send me some links, pdfs, ebooks, docs, etc for me to check them out; that woul be really great!

Thanks, Ilker GURCAN

2
  • Perhaps using <android:largeHeap="true"> in manifest can help you?
    – SteveR
    Jul 28, 2012 at 11:43
  • No i don't think so...This option is allowed for devices which have honeycomb installed to them. My application is supposed to run on froyo(Android 2.2) at minimum. Jul 28, 2012 at 12:21

1 Answer 1

0

Use ActivityManager's MemoryClass information:

Return the approximate per-application memory class of the current device. This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best. The returned value is in megabytes; the baseline Android memory class is 16 (which happens to be the Java heap limit of those devices); some device with more memory may return 24 or even higher numbers.

So, for example, you can divide by 8 the available memory to build your memory cache:

ActivityManager activityManager = (ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);
int memoryClass = activityManager.getMemoryClass();
int memCacheSize = memoryClass / 8 * 1024 * 1024; // bytes
4
  • Hi Chinaski, Thanks for your quick reply; but I wonder if really there is no way to allocate (Let's say) 50MB from the native memory and use that space to store my objects in the serialized form(Of course as efficient as possible)? I mean that; because there is no way to store my java objects on the native memory as they are. Jul 28, 2012 at 9:22
  • I think there is no way to accomplish that. At least, not an easy one. 50 MB heap is excessive, your app could damage the behaviour of the other running apps. Anyway, the info provided by getMemoryClass() method could be useful to your problem, since not all deveices have the same available memory size. Good luck!
    – Iñigo
    Jul 28, 2012 at 9:31
  • I see... One last question if you have any opinion about it... I'd seen that dolphin browser sometimes consumes 80-90MB of the memory, when i check the task manager. And as for my device(it is gingerbread) heap size limitation is 64MB per application. How could they have achieved that from your point of view? Thanks. Jul 28, 2012 at 9:39
  • Sorry, no idea. Maybe that app allocates memory from the external storage and your task manager is taking that into account? I don't know...
    – Iñigo
    Jul 28, 2012 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.