ccrama
ccrama

Reputation: 754

Need Help Managing Memory

One major issue with my application is crashing, which happens a lot due to the huge amount of content my app contains (it is a content-sharing site client). I get many memory errors, and I can be using up to 170-180 MB of ram, which is ridiculous.

http://i.gyazo.com/6cd53e6cf6f0a9bfdd6a24b323a70b09.gif http://gyazo.com/b64d50f76b2ef608954a6d6cdd5d52d0

Those screenshots are just from loading 25 submissions and scrolling through them.

My current setup is like so: LruCache with size of

(Runtime.getRuntime().maxMemory() / 1024) / 8

which handles all submission images. When I load a submission photo or thumbnail, it goes into that cache. Albums are handled by a simple ArrayAdapter and WeakHashMap store for bitmaps, because it is rarely called (maybe 1 out of every 25-30 posts contains an album). Gifs are streamed through GfyCat to a VideoView, no real crashes occur on gifs or albums. The real errors occur when I am scrolling, which is strange because I load the images into the LruCache all at once to save mobile radio time (battery improvements).

The issue seems to be that android is trying to possibly put more into the LruCache than it can, because I get errors like this

java.lang.OutOfMemoryError: Failed to allocate a 3169972 byte allocation with 1400991 free bytes and 1368KB until OOM

even though my LruCache size is 24576kb.

Am I handling memory correctly? What steps can I take to improve stability but keep the app speedy? Thanks!

Upvotes: 1

Views: 76

Answers (2)

Ziad Gholmish
Ziad Gholmish

Reputation: 871

You can also even enhance the picasso further by use this configuration

Picasso.with(this)
            .load(YOUR_URL)
            .config(Bitmap.Config.RGB_565).fit()
            .into((ImageView) findViewById(
                    R.id.frame_main_main_layout));

it will decrease allocating the memory and make the performance better

Upvotes: 1

ccrama
ccrama

Reputation: 754

I ended up switching from the Ion image loading library to Picasso, and have saved 33% of ram usage with automatic caching, so I got rid of the LruCache and all my stores, now it's working better than ever!

Upvotes: 0

Related Questions