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
Why Does Multiprocessing Use Only a Single Core After I Import Numpy
Return Value of X = Os.System(..)
How to Sort a Dictionary by Value
What Do Lambda Function Closures Capture
How to Make a Sprite Move When Key Is Held Down
Adding a Scrollbar to a Group of Widgets in Tkinter
How to Get Posted Json in Flask
Why Can a Function Modify Some Arguments as Perceived by the Caller, But Not Others
Creating a Singleton in Python
Cross-Platform Space Remaining on Volume Using Python
After Anaconda Installation, Conda Command Fails With "Importerror: No Module Named Conda.Cli"
What Does the "Yield" Keyword Do
How to Import a Module Given the Full Path
How to Make a Dictionary from Separate Lists of Keys and Values
Difference Between Old Style and New Style Classes in Python