LiquidPlayer/LiquidCore

Serious memory leak (Android liquidcore)

here-nerd opened this issue · 4 comments

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.

@ericwlange Is this project still maintained?

@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?

@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.