-
Notifications
You must be signed in to change notification settings - Fork 134
Description
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:

The memory of 'Others' kept growing. Forcing garbage collection didn't help.
Heap dump shows that the number of WeakReferences kept growing too:

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.