Product SiteDocumentation Site

4.6.3. Memory Statistics

To obtain the amount of memory currently used by the domain you can use the memoryStats method.

Example 4.44. Get the memory statistics

# Example-35.py
#!/usr/bin/env python3
import sys
import libvirt

domName = 'Fedora22-x86_64-1'

conn = None
try:
    conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
    print(repr(e), file=sys.stderr)
    exit(1)

dom = conn.lookupByID(5)
if dom == None:
    print('Failed to find the domain '+domName, file=sys.stderr)
    exit(1)

stats  = dom.memoryStats()
print('memory used:')
for name in stats:
    print('  '+str(stats[name])+' ('+name+')')

conn.close()
exit(0)
Note that the memoryStats returns a dictionary object. This object will contain a variable number of entries depending on the hypervisor and guest domain capabilities.