Skip to content

Instantly share code, notes, and snippets.

@azat
Last active December 16, 2023 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azat/ef5ab5cbbc656a033226b335e66daa6e to your computer and use it in GitHub Desktop.
Save azat/ef5ab5cbbc656a033226b335e66daa6e to your computer and use it in GitHub Desktop.
MADV_FREE vs MADV_DONTNEED: how the memory is accounted
#!/usr/bin/env python3
# systemd-run --user --unit test-madvise ./test.py
# journalctl -u -r
import mmap
import resource
import os
def get_cgroup_id():
with open("/proc/self/cgroup") as f:
content = f.read().strip()
path = content.split(':')[-1]
return path
def get_cgroup_mem(cgroup_id):
with open('/sys/fs/cgroup/' + cgroup_id + '/memory.stat') as f:
content = f.read().strip()
content = content.split('\n')
content = filter(lambda line: 'active_anon' in line, content)
content = map(lambda line: line.split(' '), content)
content = list(content)
ret = {}
for key, value in content:
ret[key] = int(value)
return ret
def get_rss():
with open('/proc/self/statm') as f:
rss = f.read().strip().split(' ')[1]
return int(rss)
def print_mem(prefix, cgroup_id):
cgroup_mem = get_cgroup_mem(cgroup_id)
rss = get_rss()
# metrics are in bytes
print('{:30}: rss {:10}, cgroup mem: inactive_anon: {}, active_anon: {}'.format(
prefix,
rss*4096,
cgroup_mem['inactive_anon'],
cgroup_mem['active_anon']))
def main():
cgroup_id = get_cgroup_id()
print('cgroup id: ' + cgroup_id)
print_mem('before mmap', cgroup_id)
vma = mmap.mmap(-1, 1<<30, mmap.MAP_PRIVATE)
print_mem('after mmap', cgroup_id)
vma.write(b'0' * int(1<<30))
print_mem('after mmap fill', cgroup_id)
vma.madvise(mmap.MADV_FREE)
print_mem('after MADV_FREE', cgroup_id)
vma.madvise(mmap.MADV_DONTNEED)
print_mem('after MADV_DONTNEED', cgroup_id)
vma.close()
print_mem('after munmap', cgroup_id)
if __name__ == "__main__":
main()
@azat
Copy link
Author

azat commented Dec 16, 2023

$ systemd-run --user --unit test-madvise ./test.py
$ journalctl -u test-madvise --user
Dec 16 14:47:45 local systemd[2171]: Started /tmp/madvise/./test.py.
Dec 16 14:47:45 local test.py[19339]: cgroup id: /user.slice/user-1000.slice/user@1000.service/app.slice/test-madvise.service
Dec 16 14:47:45 local test.py[19339]: before mmap                   : rss    9437184, cgroup mem: inactive_anon: 4096, active_anon: 4100096
Dec 16 14:47:45 local test.py[19339]: after mmap                    : rss    9437184, cgroup mem: inactive_anon: 4096, active_anon: 4100096
Dec 16 14:47:45 local test.py[19339]: after mmap fill               : rss 1083637760, cgroup mem: inactive_anon: 4096, active_anon: 1078915072
Dec 16 14:47:45 local test.py[19339]: after MADV_FREE               : rss 1083637760, cgroup mem: inactive_anon: 4096, active_anon: 6230016
Dec 16 14:47:45 local test.py[19339]: after MADV_DONTNEED           : rss    9895936, cgroup mem: inactive_anon: 4096, active_anon: 4124672
Dec 16 14:47:45 local test.py[19339]: after munmap                  : rss    9895936, cgroup mem: inactive_anon: 4096, active_anon: 4128768

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment