我无法理解应用程序中内存泄漏的情况。我有一个简单的代码,再现了如下错误:
在MainActivity
中。我创建一个日历实例并在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()
}
}
当我运行该应用程序时,只需按statBtn
,该应用程序即可正常运行(我在TextView中看到时间在更新)。但是logcat报告以下内容
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
此代码也泄漏:
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")
}
}
}