Define three different quantities first
Virtual size describes mapped address-space extent, current RSS describes resident pages, and getrusage reports a maximum RSS reached over the process lifetime. These quantities answer different questions despite often sharing the label “memory usage.” The experiment reads Linux /proc/self/statm and Python resource information for its own process only. It needs a Linux environment with procfs available but no administrator privileges. Allocate a modest sixteen-mebibyte anonymous region rather than attempting to produce an out-of-memory event. Record units explicitly before comparing the fields. A change in one quantity is not automatically a change of equal magnitude in the other two, especially across reservation, first access and release.
Record mapping, touching and release separately
Take a snapshot before allocation, immediately after mmap, after touching one byte per base page and after closing the mapping. The first two statm columns are measured in pages, so multiply by the host page size to obtain bytes. On Linux, ru_maxrss is reported in kibibytes; put that unit in the output label. Touching offsets across the whole region helps distinguish reservation from first use. The interpreter and reporting code also allocate memory within the same process. Therefore, the difference between snapshots is not an exact allocation ledger for the mapping alone. Preserve raw snapshots and explain the stages instead of subtracting unrelated fields into one supposedly authoritative memory total.
import mmap, os, resource
page = os.sysconf('SC_PAGE_SIZE')
def snapshot(label):
with open('/proc/self/statm') as stat:
size, resident, *_ = map(int, stat.read().split())
peak = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print(label, 'virtual-bytes', size * page, 'rss-bytes', resident * page, 'peak-KiB', peak)
snapshot('before')
region = mmap.mmap(-1, 16 * 1024 * 1024)
try:
snapshot('mapped')
for offset in range(0, len(region), page):
region[offset] = 1
snapshot('touched')
finally:
region.close()
snapshot('closed')Mapped size is not the same as resident allocation
Creating an anonymous mapping can establish address-space coverage before every part of that range is accessed. The kernel work at reservation and first write can therefore differ. A small immediate RSS increase does not by itself demonstrate a leak or an accounting failure. Equally, a small RSS does not prove that later accesses will require no additional memory. Writes can require new physical backing, while sharing, huge-page policy and swapping complicate the picture further. The experiment deliberately avoids an assertion that each reported field must move by exactly sixteen mebibytes. Its purpose is to reveal the distinction between the stages and the quantities, not to force environmental measurements into a predetermined total.
A high-water mark need not fall after release
A lifetime maximum normally does not decrease merely because current usage decreases. If ru_maxrss stays high after closing the region, that fact alone does not establish a failed release. Compare current mapping and RSS information separately. Linux statm residency fields can themselves be approximate because of the kernel’s accounting optimizations. More detailed interfaces such as smaps or smaps_rollup may be appropriate for a deeper investigation, at a different collection cost. Record the interface and its scope rather than deciding that two dashboards disagree because one must be wrong. They might report a peak, a current resident estimate, an address-space quantity or a differently scoped process population.
A leak diagnosis needs a longer history
One allocation-and-release cycle cannot diagnose every application memory leak. A separate investigation should ask whether repeated equivalent work leaves unrecovered growth, which objects remain reachable, and which mappings remain live. An allocator retaining reusable memory differs from application objects remaining unintentionally referenced. This small experiment establishes a vocabulary for matching units and observation times before that investigation. Keep the timestamp, operating-system and Python versions, region size and base page size beside the snapshots. Do not promise that another host will exhibit identical deltas. A useful report distinguishes what the selected interfaces observed from the further evidence required to identify a particular retention mechanism.