Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-835

Android Kotlin Realm SDK - possible memory leak

      How frequently does the bug occur?

      Always

      Description

      First of all, I'm not sure if this is a bug or I'm using the SDK on the wrong way.

      For more than a year I was using Java SDK for my Android projects (mostly in combination with Dagger-Hilt, coroutines & flows, jetpack compose) and didn't have any problem with a hight usage of memory inside my apps.

      On my newest Android project I decided to migrate to Kotlin SDK. This project is mostly based on server-driven UI (displaying full content on screens with UI provided from backend API). In a specific case - every time user changes the language in app, I need to call multiple endpoints (more than 10) to refresh that content with a new and cache everything for offline mode, I noticed that my app become significantly slower.

      Then I run some performance test with Profiler inside Android Studio and noticed that every time I'm saving something in realm database in multiple transactions, memory significantly increase and stays in RAM. First I checked that I'm doing everything fine Dagger-Hilt and that my ViewModels are getting disposed when they should. Then I thought it is something with coroutine flows and observer, but everything was fine also with them.

      I created a smaller project to check what is it going on and removed everything expect methods to save data in realm database. Here are results:

      Java SDK - app running with maximum around 170MB usage in Debug mode (on graph it is possible to see that memory is getting disposed)

      Kotlin SDK – app running with maximum around 360MB usage in Debug mode (on graph we can see that memory is only raising)

      Here is a heap memory dump graph on my larger project that is using Kotlin SDK.

      Again, I'm still not sure if there is any problem with Kotlin Realm SDK or I'm doing something wrong. Any help and information will be good, thanks!

      If you need I can create public git repository with both cases, feel free to contact me about anything.

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Always

      Reproduction Steps

      1. First I created some model with about 15 attributes
      2. In a loop with small delay, I tried to save 1000 random data of the same object in database, every saving was in it's own thread (coroutine).
      3. I done this test with both Java and Kotlin SDK (newest versions)

      Version

      1.8.0 gradle plugin

      What Atlas App Services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      Android 13

      Build environment

      Android Studio: Flamingo 2022.2.1 Patch 1
      Android Gradle Plugin Version: 7.3.1
      Gradle version: 7.4
      Kotlin version: 1.7.20
      Realm Kotlin Gradle Plugin: 1.8.0
      Compile SDK: 33
      Realm coroutines: 1.6.4

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: