简体   繁体   中英

Kotlin: Why Array and ArrayList are not emptied before a rerun?

My scientific app is fully dynamic and there is only one activity, no fragment or intent.

In some situations, I need to finish the app completely.

So I run

(this as Activity).finishAndRemoveTask()

It works smoothly since Lollipop (Android 5.0), version 21 . Apparently, no app traces or services remains in the memory.

However, I've found a huge problem.

I have some global array or array list (I didn't see other variables). that remains intact with a new rerun, even a I have a declaration that empties that variables. It doesn't happen with a rerun via debugger, should be a new rerun from cell phone.

Below I show a schematic example. I declare in one .kt file, outside any class or function.

class DispFu(
  var id: Int = 0,    
  var isKeyl:Boolean=false, 
}
var vDispFu = arrayListOf<DispFu>()  

After, I populate vDispFu inside onCreate processing and reach 134 items.

To prove it, I've recorded a file on my phone inside my activity onCreate processing, before exit the program.

fun now():String{
  var hora = LocalTime.now()
  return "${hora.hour}:${hora.minute}:${hora.second}"
}

var fileDep:File = createOrGetFile(Environment.getExternalStoragePublicDirectory
  (Environment.DIRECTORY_DOWNLOADS),"DepCalc","") // File for debugging
fileDep.appendText("${now()} -> vDispFu size is ${vDispFu.size}\n")

Below there are 2 runs: the first run (inside debugger) and a independent rerun. Here is the file content:

11:3:2 -> vDispFu size is 0
11:3:18 -> vDispFu size is 134

I know that global variables are not recommended, but I just wanted to understand what is going on. It doesn't make sense to me.

If someone could give me some clue about what is happening and give me some alternative strategy, it would be great!

Obviously, for practical reasons, the solution could not involve completely reformulating the program, which has almost 40 thousand lines...

Finishing an Activity doesn't shut down your entire Application or VM, so all global variables continue to stay in memory. When you rerun your application with the debugger, it actually restarts the VM, which is why you're seeing it get cleared in that case.

System.exit() is not an ideal solution since it restarts your VM process. There will be extra churn to do that. It's really intended for abnormal unrecoverable errors. Not necessarily a problem in your particular case.

The more proper way to handle this would be to put your top-level data in a singleton and clear it manually when you are finishing the Activity.

I've solved my problem:

Sometimes, it is incredible how the effort to externalize a problem sometimes helps to solve it ....

The below procedure to quit completely an app:

(this as Activity).finishAndRemoveTask()

It's not enough, despite all the posts about it on Stack Overflow that advocate this strategy.

It's necessary includes other command:

(this as Activity).finishAndRemoveTask()
System.exit(0)

Now:

11:26:25 -> vDispFu size is 0
11:27:1 -> vDispFu size is 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM