Get Total Physical Memory in Python

Get total physical memory in Python

your best bet for a cross-platform solution is to use the psutil package (available on PyPI).

import psutil

psutil.virtual_memory().total # total physical memory in Bytes

Documentation for virtual_memory is here.

Getting total/free RAM from within Python

Have you tried SIGAR - System Information Gatherer And Reporter?
After install

import os, sigar

sg = sigar.open()
mem = sg.mem()
sg.close()
print mem.total() / 1024, mem.free() / 1024

Hope this helps

How to measure total memory usage of all processes of a user in Python

It's pretty simple using psutil. You can just iterate over all processes, select the ones that are owned by you and sum the memory returned by memory_info().

import psutil
import getpass

user = getpass.getuser()

total = sum(p.memory_info()[0] for p in psutil.process_iter()
if p.username() == user)

print('Total memory usage in bytes: ', total)

Get memory usage of computer in Windows with Python

You'll want to use the wmi module. Something like this:

import wmi
comp = wmi.WMI()

for i in comp.Win32_ComputerSystem():
print i.TotalPhysicalMemory, "bytes of physical memory"

for os in comp.Win32_OperatingSystem():
print os.FreePhysicalMemory, "bytes of available memory"

Find total memory used by Python process and all its children

You can use the result from psutil.Process.children() (or psutil.Process.get_children() for older psutil versions) to get all child processes and iterate over them.

It could then look like:

import os
import psutil
current_process = psutil.Process(os.getpid())
mem = current_process.memory_percent()
for child in current_process.children(recursive=True):
mem += child.memory_percent()

This would sum the percentages of memory used by the main process, its children (forks) and any children's children (if you use recursive=True). You can find this function in the current psutil docs or the old docs.

If you use an older version of psutil than 2 you have to use get_children() instead of children().



Related Topics



Leave a reply



Submit