I am not able to understand how memory leak is happening in my application. I have a simple code that reproduces the error as follows:
In the MainActivity
. I create a calendar instance and update milliseconds in a TextView
private lateinit var calendar: Calendar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Example of a call to a native method
sample_text.text = stringFromJNI()
statBtn.setOnClickListener{
calendar = GregorianCalendar.getInstance()
sample_text.text = calendar.timeInMillis.toString()
}
}
When I run the application just press the statBtn
the app works fine (I see time updating in a TextView). However logcat reports the following
I/zygote64: Do partial code cache collection, code=24KB, data=26KB
I/zygote64: After code cache collection, code=24KB, data=26KB
I/zygote64: Increasing code cache capacity to 128KB
I/zygote64: Do partial code cache collection, code=57KB, data=54KB
I/zygote64: After code cache collection, code=57KB, data=54KB
I/zygote64: Increasing code cache capacity to 256KB
EDIT
This code leaks too:
package com.example.blabla.d344
import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
private val TAG = "Main Activity"
//private lateinit var task: LongTask
private var idx=0
private lateinit var calendar: Calendar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Example of a call to a native method
sample_text.text = stringFromJNI()
calendar = GregorianCalendar.getInstance()
statBtn.setOnClickListener{
//sample_text.text = calendar.timeInMillis.toString()
}
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
external fun stringFromJNI(): String
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
}
GregorianCalendar
each time you press the button?getInstance()
is not a singleton, it's a factory method, which may explain the increased memory usage until GC happens. – Michael Dodd Mar 6 at 13:58GregorianCalendar
outside theOnClickListener
the time no longer updates. AND the problem is still there, the code cache still increases. Also, why is GC not smart enough? – vvy Mar 6 at 14:01