Back to Garbage Collection & Reference Counting

Memory Management & Memory Mapping · Garbage Collection & Reference Counting · Practice Question

Cap a Process's Memory Usage with -w

Sign in to track your progress

Question

How can a q process be prevented from growing without limit, and how would you check whether it's getting close to that limit?

Solution

Start q with a workspace limit (in MB):

q -w 4000        # cap the workspace at 4000MB

Then check memory from inside the process:

q).Q.w[]
used| 10523648
heap| 67108864
peak| 67108864
wmax| 4194304000
mmap| 0
mphy| 16708280320
syms| 1822
symw| 58864

Explanation

-w is a startup flag that sets a limit on q's workspace (its heap) — once an allocation would push the heap past that limit, it fails with a 'wsfull error instead of letting the process keep growing. (Memory-mapped files are not counted against it.) This is the safety net for runaway queries or leaks in a long-running process, where the alternative is the OS eventually killing the process (or the whole machine running out of memory).

.Q.w[] reports that limit back as wmax, alongside the rest of the process's memory picture: used (heap actively in use) and heap (heap physically allocated) show current footprint, peak shows the highest heap has ever reached, and mmap shows memory currently tied up in memory-mapped files. mphy is just the machine's total physical memory, for context. syms/symw round it out with the size of the process's interned symbol pool — every symbol q has ever seen, stored once and shared by reference, which can itself become a meaningful memory cost in long-running processes that encounter a huge number of distinct symbols. \w is shorthand for the first six fields, and \w 0 shows just syms/symw on their own.