Large array - memory error
I'm writing an Android app for one specific phone (which just needs to be run on my personal phone) and I need to use the following large array that I want to use to convert RGB colors to HSV efficiently:
RainbowTable = new float[256*256*266][3];
The total size of this array should be 256 * 256 * 256 * 3 * 4B = 201326592B = 192 MB. When I debug the application, I get out of memory exception
though about 300MB of RAM is still free before executing it, according to Android Task Manager.
I have already set large-heap
-option to true
in my manifest file.
What can I do to prevent this error and save the required amount of RAM?
EDIT: my phone is rooted, so it might be possible to increase the memory heap size for each app.
Each device has a maximum cache memory for each application. If this call in the manifest doesn't ease your problem:
android:largeHeap="true"
Then your only option is to write your code with the NDK. But this is a pretty healthy thing to dive into, so I'll try to find an alternative first.
The maximum heap size is device dependent, and 192 MB is likely to be exceeded by devices currently allowed.
However, this answer indicates that you can use the NDK to allocate more memory.
If you have tried this bigHeap = true already , I doubt there is a working solution, usually the size of one memory heap can be maximum 24 - 48mb depending on the device
You can use ByteBuffer
ByteBuffer byteBuffer = ByteBuffer.allocate( bigSize);
yes, this memory issue occurs when the application is using a lot of memory. call System.gc (); this will obviously clear the garbage collector. Every app in android uses a limited memory space of around 16MB. so try this.