Code segment unload: Difference between revisions

→‎Tcl: Added description
m (omissions)
(→‎Tcl: Added description)
Line 1:
{{draft task}}[[Category:Initialization]][[Category:Memory management]]
{{draft task}}
 
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.
 
Line 8 ⟶ 7:
 
=={{header|BASIC}}==
 
Some versions of basic have the facility to delete lines of code:
 
<lang basic>10 GOSUB 5000: REM call initializer
20 DELETE 5000-9999: REM delete initializer
Line 20 ⟶ 17:
5010 LET A=1
5020 RETURN</lang>
 
=={{header|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 <code>rename</code> 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 <code>unload</code>, 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 <code>dlclose()</code> (on Unix) or <code>FreeLibrary()</code> (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>
 
{{omit from|AWK}}
Line 28 ⟶ 37:
{{omit from|TPP}}
{{omit from|UNIX Shell}}
 
[[Category:Initialization]]
[[Category:Memory management]]
Anonymous user