Detect Total Physical Memory
#1
Hi all,

I want to make a simple addon to manually set <cachemembuffersize> in the advancedsettings.xml file. I'm wondering if there's a way to detect the total physical memory in a system using python? Does Kodi store a variable for the total memory listed under System info that I can read out? I don't need to know the free memory just the total available memory in bytes. The only platform I'm concerned about getting this working on right now is Android since that's all I use Kodi on anyway.

Thanks for the help!
Reply
#2
I'm not sure if this is the best/correct way but it worked for me, tested on Win 10, returns string "8143MB"

Code:
import xbmc
mem = xbmc.getInfoLabel('system.memory(total)')
Reply
#3
Perfect, that should get me where I need to be. Now can anyone tell me how to strip out the MB on the end and covert to bytes? Regex is not my thing.. haha
Reply
#4
I would recommend you do as many of the calculations as you need to in the form of mb/kb as 32bit operating systems are limited in the size they can hold in an int and converting large numbers to bytes will result in errors, in fact the example below was run on a 64bit os (win10) and the value in "int_mem_byte" may be corrupted if run on a 32bit os.

In this example "mem_str" would be the return value of "xbmc.getInfoLabel('system.memory(total)')"

Code:
mem_str = "8143MB"
    
# simple check for MB
if mem_str.find("MB") != -1:
    # remove "MB" from string
    mem_str = mem_str.replace("MB", "")
    
    int_mem_mb = int(mem_str)
    int_mem_kb = int_mem_mb * 1024
    int_mem_byte = (int_mem_mb * 1024) * 1024
    
    print(int_mem_byte)
else:
    # not in MB
    print("oops")

remember int_mem_mb, int_mem_kb, int_mem_byte are ints not strings
Reply
#5
(2016-05-30, 22:20)diezil Wrote: I would recommend you do as many of the calculations as you need to in the form of mb/kb as 32bit operating systems are limited in the size they can hold in an int and converting large numbers to bytes will result in errors, in fact the example below was run on a 64bit os (win10) and the value in "int_mem_byte" may be corrupted if run on a 32bit os.

That's not true, for two reasons. First, the number of bytes of memory is guaranteed to fit in a single word on modern platforms, since those platforms use a single word to address each byte of memory (although you'd obviously have to use an unsigned type and handle the value 0 specially). Second, Python 2.2 and higher will transparently return a long instead of an int if the value would overflow an int (note that I'm referring to the Python types here, where int corresponds to the system's word size and long represents an integer of arbitrarily-large size).

Just for completeness, in Python 3, int and long are merged into a single type.
Reply
#6
(2016-05-31, 08:06)Pline Wrote: That's not true, for two reasons. First, the number of bytes of memory is guaranteed to fit in a single word on modern platforms, since those platforms use a single word to address each byte of memory (although you'd obviously have to use an unsigned type and handle the value 0 specially). Second, Python 2.2 and higher will transparently return a long instead of an int if the value would overflow an int (note that I'm referring to the Python types here, where int corresponds to the system's word size and long represents an integer of arbitrarily-large size).

Just for completeness, in Python 3, int and long are merged into a single type.

You'll have to excuse the python noobness as I'm still new to python, I normally code in c/arm assembly and wasn't aware that an int would overflow into long automatically and I've not read anything about unsigned ints in the python docs yet (although they do seem available through ctypes import?) so had some concerns over converting direct to bytes =)
Reply
#7
Thanks a bunch for the info guys, it worked perfectly!

Cheers!
Reply

Logout Mark Read Team Forum Stats Members Help
Detect Total Physical Memory0