Alternatives to HashMap for Android Applications

I have problems with OOM with java.util HashMap, so I'm looking for alternatives to HashMap for memory compatible with standard Java Hashmap. I tried Trove and it improved a bit, but still ends with OOM from time to time. I can accommodate some speed loss if memory is efficient.

I am not looking for a DB. HashMap file implementations are also great as long as they work offline. I keep primitives like int and byte.

Also, please indicate if you have any experience and what improvement you had in terms of memory.

+4
source share
2 answers

I can think of two options that will work for you - it depends best on your application. First, use WeakReference or SoftReference. Now keep in mind that with Android 2.3, VMs have become much more aggressive, so collecting VMs can collect these types of links more often. Personally, I'm not a big fan of using these types of links for this single reason, but some people may argue differently. The second option that I would recommend is to look in the LRU cache. You can implement your own LRU cache using LinkedHashMap. But if you use your card to store bitmaps, I would strongly suggest you take a look at the bitmap cache provided by the Android compatibility package, which includes bitmaps. Here are some links you can use to read:

Weak links

Soft links

LRU Cache with LinkedHashMap

Android Bitmap Cache and why you should avoid weak / soft links

+5
source

You should look at ArrayMap and SparseArray . These are memory efficient alternatives to using the HashMap in android.

+2
source

Source: https://habr.com/ru/post/1415494/


All Articles