memory management

memory management is the business of allocating and deallocating resources that occupy memory.

Description

In languages like C, programmers must concern themselves with direct memory management, using pointers to keep track of memory that has been allocated from the heap, and devising a strategy for determine when to deallocate the memory. Tcl shields programmers from direct memory management at the script level, but deciding when to deallocate resources that occupy memory is still very much part of the life of someone programming in higher-level languages. Most languages and systems are designed around some combination of automatic and manual memory management.

In higher-level languages, memory management is related to scope and namespaces. When a scope terminates, the values associated with that scope are deleted. When the values are not shared, the space they occupy in memory is recovered.

In Tcl every value backed by a Tcl_Obj, a C structure which typically occupies 48 bytes in memory.

See Also

copy-on-write
Understanding when a logical copy of a value in a Tcl script becomes a real copy in memory is one of the keys to effective memory management in Tcl.
garbage collection
K
contains information about how to unshare values to prevent copies, which can have a big impact on memory when values are large.
shared-memory
unshared value idiom
another page specfically about unsharing a value so that Tcl doesn't make a copy when the value is modified.
Tcl Performance
performance is often related to memory management
Dervish
Tcl library package that includes scriptable memory manager that can replace standard memory allocation functions.