LiquidPlayer / LiquidCore

Node.js virtual machine for Android and iOS
MIT License
1.01k stars 127 forks source link

Serious memory leak (Android liquidcore) #227

Open here-nerd opened 2 years ago

here-nerd commented 2 years ago

I use LiquidCore 0.7.10 in my Android app and I have experienced memory leak when calling a JS function from Kotlin code. I am sharing with you my dummy Android app to show how easy it is to reproduce the issue. I have a hard time to believe that this issue is real because that would mean liquidcore would become unusable for any Android app.. it's a time bomb. Please take a look and share your thought.

class MainActivity : AppCompatActivity() {    
   // 1000 char-long text
    private val plaintext = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.button)

        button?.setOnClickListener{
            testDummy()
        }
    }

    private fun testDummy() {
        val context = JSContext()
        context.evaluateScript(applicationContext.resources.openRawResource(
            R.raw.myjscode).bufferedReader().use { it.readText() })
        val lib = context!!.evaluateScript("myCode").toObject()
        val aFunction = lib.property("test").toFunction()
        for (i in 0..10000) {
            aFunction.call(context, plaintext).toString()
        }
    }
}

this is my javascript code:

var myCode;

myCode = {
    test: (plainText) => { }
}

Note: I use a relatively long string (1000 chars) and 10000 iterations in order to make it easily visible when memory profiling. The point is the memory consumption is growing and is not garbage collected.. even with e.g. 10 char-long string.

To reproduce the issue, run the app and keep tapping the 'RUN TEST' button --> the memory profile shows this: image The memory of 'Others' kept growing. Forcing garbage collection didn't help. Heap dump shows that the number of WeakReferences kept growing too: image

Eventually, the app would crash with this error message: "A/libc: Fatal signal 4 (SIGILL), code 2 (ILL_ILLOPN), fault addr 0xb4cbd4d4 in tid 22743 (e.myapplication), pid 22743 (e.myapplication)"

The code can be downloaded from here.

here-nerd commented 2 years ago

@ericwlange Is this project still maintained?

ericwlange commented 2 years ago

Hi. Sorry no I don't have time to maintain this anymore.

On Wed, Mar 2, 2022, 10:38 here-nerd @.***> wrote:

Is this project still maintained?

— Reply to this email directly, view it on GitHub https://github.com/LiquidPlayer/LiquidCore/issues/227#issuecomment-1056688974, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJC4ZAGXZH4WCO6QGUZ6GLU54ZJTANCNFSM5L6TFI2Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

here-nerd commented 2 years ago

@ericwlange thank you for your reply and your contributions to the project so far.

Could you please update your README sharing with your users and contributors that the project needs a new maintainer? or Would you recommend an alternative project instead?

pcfreak30 commented 2 years ago

@ericwlange Thats a shame. I just found this as a route to run a js based code base in the background on a phone for accessing the web3 internet. Could enable a lot.

Please update the project then so others are aware, but it would be nice if you got funding for this.