Jump to content

Memory allocation: Difference between revisions

Added Kotlin
(Adding C#)
(Added Kotlin)
Line 764:
 
When run using Sun's JVM implementation, the above simply outputs "created". Therefore, you cannot rely on <tt>finalize</tt> for cleanup.
 
=={{header|Kotlin}}==
In the version of Kotlin which targets the JVM, the latter takes care of memory allocation when objects are created together with the automatic deallocation of heap objects which there are no longer used via its garbage collector.
 
Consequently, manual intervention in the allocation or deallocation of objects is not possible though, as in Java (and subject to the problems mentioned in the entry therefor), it is possible to override the finalize() method to provide custom clean-up preceding garbage collection.
 
Variables of primitive types (Byte, Short, Int, Long, Float, Double, Char and Boolean) hold their values directly and variables of other types contain a reference to where the corresponding object is allocated on the heap.
 
All types (including primitive types) are either non-nullable (no suffix) or nullable (use a suffix of '?'). Only the latter can be assigned a value of 'null'. Values of nullable primitive types are 'boxed' i.e. stored as heap objects and variables of those types therefore contain a reference to the heap object rather than the value itself.
 
In addition, Kotlin has a Nothing type which has no instances and is a sub-type of every other type. There is also a nullable Nothing? type whose only value is 'null' and so, technically, this is the type of 'null' itself.
 
Some examples may help to make all this clear. In the interests of clarity, types have been specified for all variables though, in practice, this would be unnecessary in those cases where the variable's type can be inferred from the value assigned to it when it is declared. 'val' variables are read-only but 'var' variables are read/write.
<lang scala>// version 1.1.1
 
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
}</lang>
When this is run, notice that finalize() is not called - at least on my machine (running UBuntu 14.04, Oracle JDK 8):
{{out}}
<pre>
2
3
null
4
5
null
</pre>
 
=={{header|Lingo}}==
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.