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

Regression(?) infinitely growing active versions in 1.10.2 when using Singleton to retrieve realm instance

      How frequently does the bug occur?

      Always

      Description

      During some debugging I discovered that our app is suffering from infinitely growing active versions after realm write transactions.

      This is a topic with similarities to earlier issues such as https://github.com/realm/realm-kotlin/issues/1007 and https://github.com/realm/realm-kotlin/issues/1081

      Minimum reproducible example:
      With the setup below on Android, rotate the screen about 20 times (or make a button, whatever) and observe logs.

      Unable to find source-code formatter for language: kt. 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
      object Singleton { // Or Koin Singleton doesn't matter
          val config = RealmConfiguration.create(schema = setOf(Item::class))
          val realm = Realm.open(config)
      }
      
      class MainActivity : ComponentActivity() {
      
          @SuppressLint("SetTextI18n")
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
      
              repeat(100) {
                  realm.writeBlocking {
                      copyToRealm(Item().apply {
                          summary = "Do the laundry"
                          isComplete = false
                      })
                  }
              }
              println(
                  "Number of active versions: ${realm.getNumberOfActiveVersions()}"
              )
          }
      }
      
      class Item() : RealmObject {
          @PrimaryKey
          var _id: ObjectId = ObjectId()
          var isComplete: Boolean = false
          var summary: String = ""
          var owner_id: String = ""
      
          constructor(ownerId: String = "") : this() {
              owner_id = ownerId
          }
      }
      

      Log output:

      com.example.myapplication            I  Number of active versions: 101
      com.example.myapplication            I  Number of active versions: 201
      com.example.myapplication            I  Number of active versions: 301
      com.example.myapplication            I  Number of active versions: 401
      ... // Another 20+ rotations later
      com.example.myapplication            I  Number of active versions: 2501
      com.example.myapplication            I  Number of active versions: 2601
      com.example.myapplication            I  Number of active versions: 2701
      

      During rotating, I triggered several forced GCs through the Android Studio Memory profiler. However this has no effect on the number of active version.

      Reverting the realm version back to 1.6.0 (because of this comment ) and running the exact same example, garbage collection does result in the number of versions decreasing.

      Log output on v1.6.0

      com.example.myapplication            I  Number of active versions: 101
      com.example.myapplication            I  Number of active versions: 201
      com.example.myapplication            I  Number of active versions: 301
      <trigger manual GC in Android Studio>
      com.example.myapplication            I  Number of active versions: 101
      

      Interestingly, omitting the usage of the Singleton on version 1.10.2 such as this:

      Unable to find source-code formatter for language: kt. 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
       @SuppressLint("SetTextI18n")
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
      
              val realm = Realm.open(Singleton.config)
      
              repeat(100) {
                  realm.writeBlocking {
                      copyToRealm(Item().apply {
                          summary = "Do the laundry"
                          isComplete = false
                      })
                  }
              }
              println(
                  "Number of active versions: ${realm.getNumberOfActiveVersions()}"
              )
          }
      

      Also results in GCs correctly decreasing the number of active version.

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Always

      Reproduction Steps

      No response

      Version

      1.10.2

      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 version: Android Studio Flamingo | 2022.2.1 Patch 2

            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: