Couchbase Lite Android Fatal Exception: java.lang.OutOfMemoryError

I am using Couchbase Lite 1.4 version.

Sometimes I got following in my crash logs:

Fatal Exception: java.lang.OutOfMemoryError
Failed to allocate a 128 byte allocation with 4194304 free bytes and 6MB until OOM; failed due to fragmentation (required continguous free 4096 bytes for a new buffer where largest contiguous free 0 bytes)
java.util.concurrent.FutureTask.get (FutureTask.java:177)
com.couchbase.lite.replicator.RemoteRequestRetry.get (RemoteRequestRetry.java:221)
com.couchbase.lite.replicator.ReplicationInternal.waitPendingFuturesCompleted (ReplicationInternal.java:1994)
com.couchbase.lite.replicator.ReplicationInternal.waitForAllTasksCompleted (ReplicationInternal.java:1969)
com.couchbase.lite.replicator.ReplicationInternal.waitForPendingFutures (ReplicationInternal.java:1940)
com.couchbase.lite.replicator.ReplicationInternal$25.run (ReplicationInternal.java:1901)
java.lang.Thread.run (Thread.java:761)

What is the reason for such errors? How can I fix these?

This is a very old version of couchbase lite that is officially unsupported and is not actively maintained by Couchbase. Probably not the response you were looking for but would suggest upgrading to the 2.x version.

1 Like

Priya’s reply is the most important. You should upgrade to something much more recent.

I’d like to add, though, that I haven’t seen that error in a very long time. What version of Android are you running? It might be time to upgrade that as well.

As you can see, you have plenty of free memory ( 4194304 free bytes, 6MB until OOM), but the memory is so badly fragmented that you cannot get a contiguous 4K for a buffer.

I’d be surprised it that were even possible on a version of Android >=19

1 Like

Got this error on Android 7,8,9, 10 evenly distributed.

Upgradation is in our plans, but it will take time do so.

Do have mirgation instructions for such cases?

This SO answer may be useful to you.

In order to reduce jank, ART on Androids 5-7 only run compaction when some thread other than the main thread holds the processor…

1 Like

Do have mirgation instructions for such cases?

We don’t have official migration guides. But this blog should be a good start.

1 Like

Useful information, but unfotunately it seems like not my case at all, since I am running replication in the background thread (android background service).

An Android Service runs on an application’s Main/UI thread.

@Nikolay_Nikolaev did you ever find a solution, I am running into a similar issue running couchbase lite 2.8.1 on android 7.

The java.lang.OutOfMemoryError means that your program needs more memory than your Java Virtual Machine (JVM) allowed it to use.

How to Track the error?

  • Increase the default memory your program is allowed to use using the -Xmx option (for instance for 1024 MB: -Xmx1024m). By default, the values are based on the JRE version and system configuration. NOTE: Increasing the heap size is a bad solution, 100% temporary, because you will hit the same issue if you get several parallel requests or when you try to process a bigger file.

  • Find the root cause of memory leaks with help of profiling tools like MAT, Visual VM , jconsole etc. Once you find the root cause, You can fix this memory leaks.

  • Optimize your code so that it needs less memory, using less big data structures and getting rid of objects that are not any more used at some point in your program.

How to avoid this issue?

  • Use local variables wherever possible.
  • Release those objects which you think shall not be needed further.
  • Avoid creation of objects in your loop each time.
  • Try to use caches.
  • Try to move with Multy Threading.

We have migrated from CBL 1.4 to CBL 2.8+ and issue was resolved

That’s awesome, @Nikolay_Nikolaev ! Thanks for the update