本文介绍了在 Python 中确定可用 RAM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 python 脚本使用所有可用的可用 RAM,但不能使用更多(出于效率原因).我可以通过仅读取有限数量的数据来控制这一点,但我需要知道在运行时有多少 RAM 可用才能做到这一点.它将在各种 Linux 系统上运行.是否可以在运行时确定可用 RAM?

I would like my python script to use all the free RAM available but no more (for efficiency reasons). I can control this by reading in only a limited amount of data but I need to know how much RAM is free at run-time to get this right. It will be run on a variety of Linux systems. Is it possible to determine the free RAM at run-time?

推荐答案

你可以读出 /proc/meminfo.请注意,空闲内存"通常很低,因为操作系统大量使用空闲、未使用的内存进行缓存.

You could just read out /proc/meminfo. Be aware that the "free memory" is usually quite low, as the OS heavily uses free, unused memory for caching.

此外,最好不要试图超越操作系统的内存管理.这通常会以眼泪(或较慢的程序)结束.最好只使用您需要的 RAM.如果您想在以前未知内存量的机器上使用尽可能多的内存,我可能会检查安装了多少 RAM(/proc/meminfo 中的 MemTotalcode>),为操作系统留出一定数量作为安全余量(比如 1 GB)并使用其余的.

Also, it's best if you don't try to outsmart your OS's memory management. That usually just ends in tears (or slower programs). Better just take the RAM you need. If you want to use as much as you can on a machine with a previously unknown amount of memory, I'd probably check how much RAM is installed (MemTotal in /proc/meminfo), leave a certain amount for the OS and as safety margin (say 1 GB) and use the rest.

这篇关于在 Python 中确定可用 RAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:18