Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取Python中的总物理内存_Python_Linux_Memory - Fatal编程技术网

获取Python中的总物理内存

获取Python中的总物理内存,python,linux,memory,Python,Linux,Memory,如何以不依赖于分布的方式获取Python中的总物理内存?我不需要已用内存,只需要总物理内存。跨平台解决方案的最佳选择是使用该软件包(可在上获得) virtual\u memory的文档是。正则表达式适用于这类事情,可能有助于解决不同发行版之间的任何细微差异 import re with open('/proc/meminfo') as f: meminfo = f.read() matched = re.search(r'^MemTotal:\s+(\d+)', meminfo) if

如何以不依赖于分布的方式获取Python中的总物理内存?我不需要已用内存,只需要总物理内存。

跨平台解决方案的最佳选择是使用该软件包(可在上获得)


virtual\u memory的文档是。

正则表达式适用于这类事情,可能有助于解决不同发行版之间的任何细微差异

import re
with open('/proc/meminfo') as f:
    meminfo = f.read()
matched = re.search(r'^MemTotal:\s+(\d+)', meminfo)
if matched: 
    mem_total_kB = int(matched.groups()[0])
在Linux上使用

import os
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')  # e.g. 4015976448
mem_gib = mem_bytes/(1024.**3)  # e.g. 3.74
meminfo = dict((i.split()[0].rstrip(':'),int(i.split()[1])) for i in open('/proc/meminfo').readlines())
mem_kib = meminfo['MemTotal']  # e.g. 3921852
注:

  • SC\u页面大小通常为4096
  • SC_页面大小
    SC_页面大小
    相等
  • 有关详细信息,请参阅
  • 对于MacOS,根据用户报告,这适用于Python3.7,但不适用于Python3.8
在Linux上使用
/proc/meminfo

import os
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')  # e.g. 4015976448
mem_gib = mem_bytes/(1024.**3)  # e.g. 3.74
meminfo = dict((i.split()[0].rstrip(':'),int(i.split()[1])) for i in open('/proc/meminfo').readlines())
mem_kib = meminfo['MemTotal']  # e.g. 3921852

在Python 2.7.9中,这段代码在没有任何外部库的情况下为我工作

import os
mem=str(os.popen('free -t -m').readlines())
"""
Get a whole line of memory output, it will be something like below
['             total       used       free     shared    buffers     cached\n', 
'Mem:           925        591        334         14         30        355\n', 
'-/+ buffers/cache:        205        719\n', 
'Swap:           99          0         99\n', 
'Total:        1025        591        434\n']
 So, we need total memory, usage and free memory.
 We should find the index of capital T which is unique at this string
"""
T_ind=mem.index('T')
"""
Than, we can recreate the string with this information. After T we have,
"Total:        " which has 14 characters, so we can start from index of T +14
and last 4 characters are also not necessary.
We can create a new sub-string using this information
"""
mem_G=mem[T_ind+14:-4]
"""
The result will be like
1025        603        422
we need to find first index of the first space, and we can start our substring
from from 0 to this index number, this will give us the string of total memory
"""
S1_ind=mem_G.index(' ')
mem_T=mem_G[0:S1_ind]

print 'Summary = ' + mem_G
print 'Total Memory = ' + mem_T +' MB'
我们可以很容易地获得已用内存和空闲内存

    """
        Similarly we will create a new sub-string, which will start at the second value. 
        The resulting string will be like
        603        422
        Again, we should find the index of first space and than the 
        take the Used Memory and Free memory.
        """
        mem_G1=mem_G[S1_ind+8:]
        S2_ind=mem_G1.index(' ')
        mem_U=mem_G1[0:S2_ind]

        mem_F=mem_G1[S2_ind+8:]
    print 'Used Memory = ' + mem_U +' MB'
    print 'Free Memory = ' + mem_F +' MB'

您可以阅读
/proc/meminfo
/proc/meminfo
应该可以在几乎所有的linux安装上使用。您不需要
/proc/meminfo
,因为./proc仅在CONFIG\u proc\u FS=y时可用。适用于台式机、服务器、手机,但嵌入式设备并不总是如此。你几乎不应该关心物理内存(例如,通过docker等想象一个虚拟处理器。)@sorin,
os.sysconf('SC\u PHYS\u PAGES')
显然在os X上不起作用。尽管os X的手册页确实记录了
\u SC\u PHYS\u PAGES
,这似乎无法通过Python实现。使用
psutil
,您可能会更幸运。或者,请参考答案中使用的技术。FWIW,
os.sysconf()
方法也适用于Python 3,甚至适用于Solaris 10。我很高兴地报告,它适用于MacOS X 10.13(High Sierra)和Python 3.6.4。
/proc/meminfo
在AWS上似乎没有预期的效果:在MacOS上,在Python3.7上运行良好,但在Python3.8.3上不起作用。获取
ValueError:os.sysconf('SC\u PHYS\u PAGES')
/proc的配置名称无法识别,仅在Linux上存在。。。所以不是跨平台的我把分布不可知论误认为是跨平台的。。。现在我看到它被标记为Linux。我的坏:)
/proc/meminfo
似乎无法在AWS上按预期工作:@joshuadetwiller使用此类上下文时,不需要显式关闭文件。当上下文(with块)关闭时,文件将与它一起关闭。另外@martin thoma您使用的AWS图像是什么,最新的amazonlinux2没有这个问题。
mem=virtual_memory()
,然后是
#总物理内存
?听起来很棒,2018年12月18日证明了这一点:Mac上默认不安装免费软件