'psutil virtual memory units of measurement?

When running psutil.virtual_memory() i'm getting output like this:

    >>psutil.virtual_memory()
    vmem(total=8374149120L, available=1247768576L)

But what unit of measurement are these values? The documentation simply claims that its the "total physical memory available" but nothing more. I'm trying to translate it into values that the user can actually relate to (ie GBs).

Thanks in advance



Solution 1:[1]

why not use bit shift operator: if you want to display in human readable way, just like this!

values = psutil.virtual_memory()

display in MB format

total = values.total >> 20

display in GB format

total = values.total >> 30

Solution 2:[2]

1024^3 = Byte to Gigabyte
So I think this work:

import psutil
memory = psutil.virtual_memory().total / (1024.0 ** 3)
print(memory)

Solution 3:[3]

The unit of measurement specified is bytes . You can use this code to convert it into Gb's When u use the value it will have a trailing "L" , but that doesn't affect the calculations.

values=psutil.virtual_memory()
def get_human_readable_size(self,num):
    exp_str = [ (0, 'B'), (10, 'KB'),(20, 'MB'),(30, 'GB'),(40, 'TB'), (50, 'PB'),]               
    i = 0
    while i+1 < len(exp_str) and num >= (2 ** exp_str[i+1][0]):
        i += 1
        rounded_val = round(float(num) / 2 ** exp_str[i][0], 2)
    return '%s %s' % (int(rounded_val), exp_str[i][1])
total_size = get_human_readable_size(values.total)

Solution 4:[4]

It is in Bytes. To convert to a more readable format, simply use bytes2human

import psutil
from psutil._common import bytes2human

mem_usage = psutil.virtual_memory()
total_in_human_format = bytes2human(mem_usage[0])
print(total_in_bytes)

Output:

15.6G

Solution 5:[5]

Cant comment so I'm using "answer".

regarding "1024^3 = Byte to Gigabyte"

This is incorrect. The prefix Giga means 10^9. Therefore, a Gigabyte is 1000^3 Bytes. You can forget the extra 24.

Therefore: 1000^3 = Byte to Gigabyte

Solution 6:[6]

If you don't mind a 3rd party dependency, and you want to avoid magic numbers in your code, try pint.

This lets you work with units symbolically, and you can convert to whatever you want and get the "magnitude" at the end of your computation.

I like this because the code "self-documents" that the info from psutil is in bytes and then self-documents that we are converting to gigabytes.

            import psutil
            import pint
            reg = pint.UnitRegistry()
            vmem_info = psutil.virtual_memory()
            total_gb = (vmem_info.total * reg.byte).to(reg.gigabyte).m
            avail_gb = (vmem_info.available * reg.byte).to(reg.gigabyte).m
            print('total_gb = {!r}'.format(total_gb))
            print('avail_gb = {!r}'.format(avail_gb))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gourneau
Solution 2
Solution 3
Solution 4
Solution 5 abby yorker
Solution 6 Erotemic