realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
934 stars 57 forks source link

Android Kotlin Realm SDK - possible memory leak #1402

Closed siserivan closed 9 months ago

siserivan commented 1 year ago

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) image

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

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

cmelchior commented 1 year ago

Hi @siserivan Thank you for reporting this. This looks similar to https://github.com/realm/realm-kotlin/issues/1393 which we are already looking into.

But if you have the opportunity to provide a small sample Kotlin project that would be very helpful so we are 100% certain we are measuring the same thing.

nhachicha commented 10 months ago

Hi @siserivan we did recently a couple of memory leak fixes, could you please try this again with the latest version?

github-actions[bot] commented 9 months ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.