0

In my android app I'm allocating a large int array. This sometimes gives me an OutOfMemory error, when I think there should be sufficient memory. This is an example of what I get:

// always this value
ActivityManager.some_instance.getMemoryClass() = 128 Mb

// always this value
Runtime.getRuntime().maxMemory()/(1024*1024) = 128 Mb

// example value
Runtime.getRuntime().freeMemory()/(1024*1024) = 81 Mb

// example value, can also be bigger than freeMemory()
arrayLengthToAllocate*4/(1024*1024) = 47 Mb 

To be clear, I get the OutOfMemory error for situations where the last value is larger or smaller than freeMemory().

Why do I get the error? Is the heap size not increased when the allocation is performed? The memory use of the app just before trying to allocate is about 8 Mb, so that cannot be the problem.

PS. Other approaches than using an int[] of what this app is doing are not possible.

3 Answers 3

1

Generally speaking most devices have a cap on the amount of memory you can use. I know that originally this was set at 16MB per application. I think this has increased since, but it is still something you should be taking into account.

You also have to bare in mind that your applications footprint can fluctuate and that the Android Operating System has to give memory to other applications as well which are competing for resources. It may be that Android is identifying your application as being a resource hog and is cutting back it's resources.

Storing 47MB of integers in memory cannot be a very good use of an application's memory, is there no easier approach? I fail to see why you would need so many integers to be so readily available. Surely adding the integers to a SQLite database, reading them in as required, thus moving the bulk of the memory requirements away from RAM and onto physical memory would be a better approach?

When you are hitting OutOfMemory errors it is normally an indication that you are being a bit too greedy and the best way of limiting them is to scale back what you are doing, find a different approach.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your anwere. The data needs to be readily available as it will contain image data on which live warping to an on-screen image is applied. The error occurs at allocation of the array, not at later image stuff. Any idea of how to tell java that I'm not greedy, but just needy?
0

The memory amount you get is the system memory size. Each application has a limited subset of that, which depends on a few system variables. In general it's something in the order of 16 - 32 MB, sometimes a bit bigger for tablets.

2 Comments

I also agree with biddulph.r that 47 MB of integers sounds wrong.
Thank you, but getMemoryClass() is said to return the per-application memory.
0

I figured out that the problem is heap fragmentation. Defragmentation has limits such that there are simply not enough free consecutive bytes in the heap to fit the single large array. I restructered the data into a two-dimensional array which solves the problem as each row has its own location in the heap. Downside is that retrieving the data from the array is slightly slower.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.