Code segment unload: Difference between revisions

Added Kotlin
(Added Io entry.)
(Added Kotlin)
Line 38:
<pre>Initialization complete.
Doing real work.</pre>
 
=={{header|Kotlin}}==
The principal version of Kotlin targets the JVM whose basic code unit is the 'class'. Each class has (and carries a reference to) a classloader which loads it into memory as needed. In most cases the classloaders are created automatically by the system. However, it is possible to create your own to enable classes to be loaded dynamically at runtime.
 
To my knowledge, there is no explicit mechanism for unloading a class. However, when there are no longer any references to any of its classes or a classloader itself, they will become eligible for garbage collection and the memory they were using will in due course be reclaimed by the garbage collector.
 
In most cases this is good enough as one of the tenets of the system is that the programmer is relieved from having to worry about memory management.
 
Even so, situations can arise where one would like a class to be unloaded as quickly as possible and a strategy for dealing with this is to create a classloader which will only load a single class (or a small number of classes) and to then carefully manage the creation of references so that you know when it is safe to unload it (or them). You can then request an immediate garbage collection by calling System.gc(). Note though that this is not guaranteed to work as the request may be overridden by the system if it is deemed to be inopportune.
 
=={{header|PARI/GP}}==
9,482

edits