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

KMM Memory Leak (RAM) issue

      How frequently does the bug occur?

      Always

      Description

      I have been using Java Realm in the past for an Android app, that I migrated recently to Kotlin Multiplatform in order to make it compatible with iOS. After testing, it appears that by running it in an iOS environment, my memory was getting saturated by a leak, which affected the RAM. So I made a really simple downgraded version of it which showcases what exactly fails in it. In short, by calling the realm.open and realm.close methods, I leak memory (instances of realm_scheduler according to my memory graph on Xcode, but anything related to realms seems to create a way too large amount of instances in it).

      In short, my Swift code to be run on iOS only executes this, which creates a timer calling Leaker.leak() every .1s :

          var leaker = RealmLeaker()
          // Code skipped here for readability
              NavigationView {
                  Text("A")
              }
              .onAppear() {
                  var timer1 = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timerI in
                      // Leaking
                      leaker.leak()
                  }
              }
      

      In the KMM side, here's the Leaker Class :

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class RealmLeaker {
          fun leak() {
              val config = RealmConfiguration.Builder(setOf(TestClass::class))
                  .build()
      
              // This should simply open, then close the realm. Note that if you comment everything below this, there is no leak.
              val realm: Realm = Realm.open(config)
              realm.close()
          }
      }
      

      And for more context, here's the "TestClass" that I use :

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      // This class isn't really useful. Only there to showcase the leak, its properties are outside the scope of the problem.
      class TestClass : RealmObject{
          @PrimaryKey
          var _id : String = "0"
      
          var value : Boolean = false
          var key : String = "test"
      }
      

      Here’s a repo to reproduce the error : GitHub - ALXgba/realmKMMIssue: Memory leak using Kotlin API in a KMM project, when run in iOS environment. 1

      This issue seemed to be previously patched, yet I can still reproduce it : https://github.com/realm/realm-kotlin/pull/1463#pullrequestreview-1564671369

      Edit : While the issue doesn't seem so terrible using the Xcode debug graphs, the RAM used seems to grow at a small rate, but I have observed it in my actual app (not the test one) growing fast enough to crash after an hour or so as the whole RAM gets clogged up.

      Can you reproduce the bug?

      Always

      Reproduction Steps

      Open and close a realm instance from a KMM Project using Realm Kotlin SDK inside your Swift iOS project. Everytimes you do this, a memory leak occurs.

      Version

      1.11.0

      Are you using encryption?

      No

      Platform OS and version(s)

      iOS 16.6 | Android SDK 26 and up

            Assignee:
            clemente.tort@mongodb.com Clemente Tort Barbero
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: