Sourcent
Sourcent

Reputation: 21

How to find only the total RAM in Python

I am creating a bunch of Python functions for use in a later script and they find things like free RAM, Total RAM, and RAM in use. I want to only find the number, nothing else. Is there a Python module or something to find only these numbers? I have tried psutil, but that is not what I want. I am using Linux, a terminal command would work as well. With your answer, please keep it simple, because I am a new programmer.

Upvotes: 1

Views: 538

Answers (2)

Ajay2588
Ajay2588

Reputation: 567

You can use free command with option -g. This is will give a snapshot the memory in GB

[email protected].:~# free -g
             total       used       free     shared    buffers     cached
Mem:            31         24          6          0          0         14
-/+ buffers/cache:         10         21
Swap:           31          0         31

Upvotes: 0

Jahid
Jahid

Reputation: 22438

This will give you the total memory:

grep MemTotal /proc/meminfo |grep -oE "[0-9]*"

This one free memory:

grep MemFree /proc/meminfo |grep -oE "[0-9]*"

This one Active memory:

grep Active /proc/meminfo |grep -oE "[0-9]*"

You can extract all the information you need from /proc/meminfo like this.

To see what options do you have, run cat /proc/meminfo

Upvotes: 1

Related Questions