androidmemoryout-of-memoryfragmentation

Out of memory error android due to fragmentation after 20+days


I made a application that runs on a coffee machine.
After 20+ days (can be 60+ days depending on use) an OutOfMemoryError occurs:

    java.lang.OutOfMemoryError: Failed to allocate a 604 byte allocation with 16777216 free bytes and 319MB until OOM; failed due to fragmentation (required continguous free 65536 bytes for a new buffer where largest contiguous free 53248 bytes)

My question is:
Is there a way to run a defragmentation on memory android application programmatically?

The time it takes should not be a issue because machine goes into standby or eco mode.
And what I see is that there is more than enough memory available.


Solution

  • Is there a way to run a defragmentation on memory android application programmatically?

    No. On Android 5.0-7.1, the best thing that you can do is get out of the foreground, as ART's garbage collector will compact memory only when your app is in the background. On Android 8.0+, ART's garbage collector will compact memory even while you are in the foreground.

    Beyond that, aim to start a fresh process once per week or something, so you get a fresh heap.