September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
30
Task/Memory-allocation/Kotlin/memory-allocation.kotlin
Normal file
30
Task/Memory-allocation/Kotlin/memory-allocation.kotlin
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// version 1.1.2
|
||||
|
||||
class MyClass(val myInt: Int) {
|
||||
// in theory this method should be called automatically prior to GC
|
||||
protected fun finalize() {
|
||||
println("MyClass being finalized...")
|
||||
}
|
||||
}
|
||||
|
||||
fun myFun() {
|
||||
val mc: MyClass = MyClass(2) // new non-nullable MyClass object allocated on the heap
|
||||
println(mc.myInt)
|
||||
var mc2: MyClass? = MyClass(3) // new nullable MyClass object allocated on the heap
|
||||
println(mc2?.myInt)
|
||||
mc2 = null // allowed as mc2 is nullable
|
||||
println(mc2?.myInt)
|
||||
// 'mc' and 'mc2' both become eligible for garbage collection here as no longer used
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
myFun()
|
||||
Thread.sleep(3000) // allow time for GC to execute
|
||||
val i: Int = 4 // new non-nullable Int allocated on stack
|
||||
println(i)
|
||||
var j: Int? = 5 // new nullable Int allocated on heap
|
||||
println(j)
|
||||
j = null // allowed as 'j' is nullable
|
||||
println(j)
|
||||
// 'j' becomes eligible for garbage collection here as no longer used
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue