title: How to cache bitmaps into native memory
body: <p>For my 10,000 points, i've decided to contibute something with this cool website: a mechanism to cache bitmaps on native memory.</p>
  
  <h2>Background</h2>
  
  <p>Android devices have a very limited amount of memory for each app- the heap ranges from 16MB to 128MB , depending on <a href="https://stackoverflow.com/a/17770158/878126"><strong>various parameters</strong></a> .</p>
  
  <p>If you pass this limit, you get OOM , and this can occur many times when you use bitmaps.</p>
  
  <p>Many times, an app might need to overcome those limitations, perform heavy operations on huge bitmaps or just store them for later use, and you need to </p>
  
  <p>What i've come up with, is a simple java class that would make things a bit easier for those purposes.</p>
  
  <p>It's using JNI in order to store the bitmap data , and be able to restore it when needed.</p>
  
  <p>In order to support multiple instances of the class, i had to use a trick i've found (<a href="https://stackoverflow.com/a/17900353/878126"><strong>here</strong></a>) . </p>
  
  <h2>Important notes</h2>
  
  <ul>
  <li><p>The data is still stored on the RAM, so if the device doesn't have enough RAM, the app might be killed.</p></li>
  <li><p>Remember to free the memory as soon as you can. it's not only to avoid memory leaks, but it's also in order to avoid being prioritized by the system to be killed first, once your app comes to the background.</p></li>
  <li><p>If you don't want to forget freeing the memory, you can either free it each time you restore the bitmap, or make the class implement <a href="http://developer.android.com/reference/java/io/Closeable.html" rel="noreferrer"><strong>Closable</strong></a> . </p></li>
  <li><p>As a safety measure, i've made it automatically free its native memory in the finalize() method, but don't let it be responsible of the job. it's too risky. i've also made it write to the log when such a thing occurs.</p></li>
  <li><p>The way it works is by copying the entire data into JNI objects, and in order to restore, it creates the bitmap from scratch and puts the data inside.</p></li>
  <li><p>Bitmaps being used and restored are in ARGB_8888 format. of course, you can change it to whatever you wish, just don't forget to change the code...</p></li>
  <li><p>Large bitmaps could take time to store and restore, so it might be wise to do it on a background thread.</p></li>
  <li><p>This is not a full OOM solution, but it could help. for example, you could use it in conjunction with your own LruCache, while avoiding using the heap memory for the cache itself.</p></li>
  <li><p>Code is only for storing and restoring. if you need to perform some operations, you will need to perform some research. <a href="http://opencv.org/" rel="noreferrer"><strong>openCV</strong></a> might be the answer, but if you wish to perform some basic stuff, you could implement them by yourself (<strong><a href="https://stackoverflow.com/questions/14398670/rotating-a-bitmap-using-jni-ndk">here's an example</strong></a> of rotatign large images using JNI). if you know other alternatives, please let me know, <a href="https://stackoverflow.com/questions/17885201/image-manipulation-and-handling-on-the-jni-side-of-android"><strong>here</strong></a> .</p></li>
  </ul>
  
  <p>Hope this will be useful for some people. please write down your comments.</p>
  
  <p>Also, if you find any problem with the code or a suggestion for impovement, please let me know.</p>
  
  <hr>
  
  <h2>Better solution</h2>
  
  <p>If you wish to perform even more operations on the JNI side, you can use <a href="https://stackoverflow.com/questions/18250951/jni-bitmap-operations-for-helping-to-avoid-oom"><strong>this post</strong></a> that I've made. it's based on the code I've written here, but allows you to do more operations and you can easily add more of your own.</p>