memory(n)

πŸ’– NAME πŸ’–

okay so bestie, welcome to memory β€” the Tcl command that lets you control Tcl's memory debugging capabilities. that's it, that's the whole vibe. it's the little backstage pass to the memory subsystem and honestly she's iconic for it.

this page lives in the Tcl Built-In Commands section of the manual (that's the (n) after her name, iykyk). 🫢


✨ SYNOPSIS ✨

memory option ?arg arg ...?

🧚 DESCRIPTION 🧚

the memory command hands the Tcl developer the keys to Tcl's memory debugging capabilities. it's got a whole bunch of suboptions, all listed down below, and they are, no exaggeration, giving main character energy. πŸ’…

real talk though: this command is only available when Tcl has been compiled with memory debugging enabled (that means TCL_MEM_DEBUG is defined at compile time), and after Tcl_InitMemory has been called. no compile flag, no command. periodt.

🌸 the suboptions 🌸

  • memory active file β€” writes a list of all currently allocated memory into the file you name. the whole inventory. every byte, accounted for.
  • memory break_on_malloc count β€” after count allocations have been performed, Tcl_Alloc outputs a message to this effect and says it's now attempting to enter the C debugger. then Tcl issues a SIGINT signal against itself. so if you're running Tcl under a C debugger, it should pop you right into the debugger command mode. dramatic? yes. useful? also yes. 😭
  • memory info β€” returns a report containing:
    • the total allocations and frees since Tcl began
    • the current packets allocated (that's the current number of calls to Tcl_Alloc not met by a corresponding call to Tcl_Free)
    • the current bytes allocated
    • the maximum number of packets and bytes allocated

she's a full report card. πŸ“‹

  • memory init [on|off] β€” turns the preinitialization of all allocated memory with bogus bytes on or off. super useful for detecting the use of uninitialized values. (bogus bytes are basically little tripwires, love that for us. ✨)
  • memory objs file β€” causes a list of all allocated Tcl_Obj values to be written to the specified file immediately, together with where they were allocated. useful for checking for leaks of values. πŸ•΅οΈβ€β™€οΈ
  • memory onexit file β€” causes a list of all allocated memory to be written to the specified file during the finalization of Tcl's memory subsystem. useful for checking that memory is properly cleaned up during process exit. she tidies up before she leaves, as she should. πŸŽ€
  • memory tag string β€” every packet of memory allocated by Tcl_Alloc can have a string-valued tag associated with it. in the lists of allocated memory generated by memory active and memory onexit, the tag for each packet is printed along with other information about the packet. the memory tag command sets the tag value for subsequent calls to Tcl_Alloc to be string. it's giving name tags at a party, honestly. 🏷️
  • memory trace [on|off] β€” turns memory tracing on or off. when memory tracing is on, every call to Tcl_Alloc causes a line of trace information to be written to stderr, consisting of the word Tcl_Alloc, followed by the address returned, the amount of memory allocated, and the C filename and line number of the code performing the allocation. for example:
Tcl_Alloc 40e478 98 tclProc.c 1406

calls to Tcl_Free are traced in the same manner. πŸ”

  • memory trace_on_at_malloc count β€” enable memory tracing after count Tcl_Allocs have been performed. for example, if you enter memory trace_on_at_malloc 100, after the 100th call to Tcl_Alloc, memory trace information will begin being displayed for all allocations and frees. since there can be a lot of memory activity before a problem occurs, judicious use of this option can reduce the slowdown caused by tracing (and the amount of trace information produced), if you can identify a number of allocations that occur before the problem sets in. the current number of memory allocations that have occurred since Tcl started is printed on a guard zone failure. πŸ”₯
  • memory validate [on|off] β€” turns memory validation on or off. when memory validation is enabled, on every call to Tcl_Alloc or Tcl_Free, the guard zones are checked for every piece of memory currently in existence that was allocated by Tcl_Alloc. this has a large performance impact and should only be used when overwrite problems are strongly suspected. 😬 the advantage of enabling memory validation is that a guard zone overwrite can be detected on the first call to Tcl_Alloc or Tcl_Free after the overwrite occurred, rather than when the specific memory with the overwritten guard zone(s) is freed β€” which may occur long after the overwrite occurred. so yeah, slow but psychic. pick your fighter. πŸ’€

πŸ‘‘ SEE ALSO πŸ‘‘

  • Tcl_Alloc
  • Tcl_Free
  • Tcl_ValidateAllMemory
  • Tcl_DumpActiveMemory
  • TCL_MEM_DEBUG

🌈 KEYWORDS 🌈

memory, debug


okay bestie that's the whole page, you ate it, go debug something and slay. πŸ’œ

this was the memory(n) page from the Tcl Built-In Commands manual β€” Tcl 8.1, dated... well, whenever 8.1 said so. mwah, no notes. πŸ«Άβœ¨πŸŽ€

πŸ“¦ where this page comes from