Code segment unload: Difference between revisions

From Rosetta Code
Content added Content deleted
(Omitted Mathematica)
(Added Io entry.)
Line 21: Line 21:
The <code>CANCEL</code> statement in COBOL unloads from memory the dynamically-loadable module containing the specified program or entry-point.
The <code>CANCEL</code> statement in COBOL unloads from memory the dynamically-loadable module containing the specified program or entry-point.
<lang cobol>CANCEL "foo"</lang>
<lang cobol>CANCEL "foo"</lang>

=={{header|Io}}==
<lang Io># Call anonymous block to do the initialization.

block(
// Put initialization code here.
writeln("Initialization complete.")
) call()

# Garbage collector is now free to recover initialization resources.

writeln("Doing real work.")
// Code to do the real work here.</lang>

{{output}}
<pre>Initialization complete.
Doing real work.</pre>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==

Revision as of 21:42, 7 October 2016

Code segment unload is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Some advanced applications, such as memory resident applications, daemons and memory demanding applications unload unrequired parts of the application during use (such as initialization code that will no longer be used again once the application is operational) to free up memory and resources. Demonstrate or explain how to unload an unused section of code from memory.

Note that it is sufficient to flag the memory area as being available for reuse by the memory management system, without the need to demonstrate the zeroing of the bytes and performing a memory shuffle, unless such actions are required to free up the code segment.

Languages that do not have the facility to free up their code segment (such as scripting languages) can be omitted.

BASIC

Some versions of basic have the facility to delete lines of code: <lang gwbasic>10 GOSUB 5000: REM call initializer 20 DELETE 5000-9999: REM delete initializer 30 PRINT A: REM show initializer worked 40 LIST: REM show initializer has gone 50 END

5000 REM this is a dummy initializer 5010 LET A=1 5020 RETURN</lang>

COBOL

The CANCEL statement in COBOL unloads from memory the dynamically-loadable module containing the specified program or entry-point. <lang cobol>CANCEL "foo"</lang>

Io

<lang Io># Call anonymous block to do the initialization.

block(

   // Put initialization code here.        
   writeln("Initialization complete.")

) call()

  1. Garbage collector is now free to recover initialization resources.

writeln("Doing real work.") // Code to do the real work here.</lang>

Output:
Initialization complete.
Doing real work.

PARI/GP

PARI code can be unloaded like any other C code. But if the code is in the form of a closure (i.e., a GEN object of type t_CLOSURE) then its space should be reclaimed by gerepile or moving ltop if it's at the bottom of the stack.

This is simpler in gp -- just kill the function (or set it to 0, not quite the same but will also cause the memory to be collected).

Python

The del statement can make objects (both code and data), available for reuse.

Racket

Racket has a JIT compiler that translates functions to machine code whenever they are applied. When such a function is garbage-collected, the JITted machine code is released or re-used with it. Therefore, to reclaim some executable code segment, simply drop references to the function.

Tcl

Tcl can release memory associated with a program in three ways.

Releasing commands
The memory associated with a particular command can be released back to the general memory pool by deleting the command. This is done by either creating a new command with the same name, or by using rename to change the command's name to the empty string.
<lang tcl>rename exampleCmd ""</lang>
Releasing loaded extensions
The memory associated with a loaded extension can be released by using unload, provided the extension has registered a handler function (this is relatively uncommon). Once the handler function has run (which gives the extension an opportunity to destroy any commands and other callbacks it has created), the underlying library will be removed from memory with dlclose() (on Unix) or FreeLibrary() (on Windows). This completely removes the program code concerned, as well as returning the other ancillary memory to the general pool.
<lang tcl>unload theLibrary.dll</lang>
Releasing an entire interpreter
Provided an interpreter is not the root interpreter in its thread, you can delete it from an ancestor interpreter, which releases all the memory associated with it back into the general memory pool.
<lang tcl>interp delete theChildInterpreter</lang>