Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KMM Memory Leak (RAM) issue #1501

Closed
ALXgba opened this issue Sep 5, 2023 · 7 comments
Closed

KMM Memory Leak (RAM) issue #1501

ALXgba opened this issue Sep 5, 2023 · 7 comments

Comments

@ALXgba
Copy link

ALXgba commented Sep 5, 2023

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 :

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 :

// 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 : #1463 (review)

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

@ALXgba ALXgba added the T-Bug label Sep 5, 2023
@ALXgba ALXgba changed the title Realm Memory Leak (RAM + Disk) issue KMM Realm Memory Leak (RAM + Disk) issue Sep 5, 2023
@ALXgba ALXgba changed the title KMM Realm Memory Leak (RAM + Disk) issue KMM realm_scheduler Memory Leak (RAM + Disk) issue Sep 6, 2023
@ALXgba ALXgba changed the title KMM realm_scheduler Memory Leak (RAM + Disk) issue KMM Memory Leak (RAM + Disk) issue Sep 21, 2023
@ALXgba
Copy link
Author

ALXgba commented Sep 22, 2023

I use the SDK locally, but I don't know how to change the tags of the ticket. Could someone please edit it?

@JustusBachmann
Copy link

Hi, i have the same problem in my kmm project. It seems that the realm-kotlin/releases branch provides a fix for this, is there any chance that the correction will be officially released soon? @clementetb

@clementetb
Copy link
Contributor

Hi @JustusBachmann, we have addressed the issue here.

We have no planned release date yet, but you can use the snapshot builds. Please report back if the fix is working for you.

@ALXgba
Copy link
Author

ALXgba commented Oct 26, 2023

Hi @JustusBachmann, we have addressed the issue here.

We have no planned release date yet, but you can use the snapshot builds. Please report back if the fix is working for you.

Hello @clementetb. I have tried the snapshot on my test project, and I can confirm that it works. I hope it will come soon in an official release to integrate in my project. :)

@ALXgba
Copy link
Author

ALXgba commented Oct 30, 2023

Regarding the disk memory leak that I reported, that was an error on my part. Xcode was logging every read and writes to the disk in a file that wasn't cleaned, thus making it seem as if it was the database leaking. I will edit the issue's original description to reflect that.

@ALXgba ALXgba changed the title KMM Memory Leak (RAM + Disk) issue KMM Memory Leak (RAM) issue Oct 30, 2023
@clementetb
Copy link
Contributor

@ALXgba Ok, but the original issue was fixed as you stated in #1501 (comment)?

By the way, the fix is available in 1.12.0

@ALXgba
Copy link
Author

ALXgba commented Nov 14, 2023

@ALXgba Ok, but the original issue was fixed as you stated in #1501 (comment)?

Yes, the issue has been resolved in 1.12.0, and I have tested it.

@ALXgba ALXgba closed this as completed Nov 14, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants