简体   繁体   English

如何找到在 Linux 环境中可用的用户空间内存量?

[英]How to find how much userspace memory available to use in Linux environment?

I am using CONFIG_VMSPLIT_1G config in my 32-bit Linux image.我在我的 32 位 Linux 映像中使用CONFIG_VMSPLIT_1G配置。 so 3GB of virtual address space is allocated to kernel and 1GB of virtual address space to userspace.所以3GB的虚拟地址空间分配给内核,1GB的虚拟地址空间分配给用户空间。 Now if i run stressapptest in userspace with 80% of the available memory shown using cat /proc/meminfo , it throws an error saying "failed to allocate memory".现在,如果我在使用 cat /proc/meminfo显示的可用内存的 80% 的用户空间中运行 stressapptest,它会抛出一个错误,提示“无法分配内存”。

Is there any way to find how much excat amount of virtual address space is allocated for userspace?有什么方法可以找到为用户空间分配了多少虚拟地址空间?

root@:/#
root@:/# cat /proc/meminfo | head -5
MemTotal:        1826896 kB
MemFree:         1708308 kB
MemAvailable:    1694060 kB
Buffers:            2484 kB
Cached:             9520 kB
root@:/#
root@:/# ./stressapptest -M 900
2021/09/25-06:48:59(UTC) Log: Commandline - ./stressapptest -M 900
2021/09/25-06:48:59(UTC) Stats: SAT revision 1.0.7_autoconf, 32 bit binary
2021/09/25-06:48:59(UTC) Log: varada @ CHEPSSW01 on Wed Aug 27 12:05:13 IST 2014 from open source release
2021/09/25-06:48:59(UTC) Log: 1 nodes, 4 cpus.
2021/09/25-06:48:59(UTC) Log: Defaulting to 4 copy threads
2021/09/25-06:48:59(UTC) Log: Flooring memory allocation to multiple of 4: 900MB
2021/09/25-06:48:59(UTC) Log: Prefer plain malloc memory allocation.
2021/09/25-06:48:59(UTC) Process Error: memalign returned 0
2021/09/25-06:48:59(UTC) Process Error: failed to allocate memory
2021/09/25-06:48:59(UTC) Process Error: Sat::Initialize() failed
2021/09/25-06:48:59(UTC)
2021/09/25-06:48:59(UTC) Status: FAIL - test encountered procedural errors
2021/09/25-06:48:59(UTC)
2021/09/25-06:48:59(UTC) Process Error: Fatal issue encountered. See above logs for details.
root@OpenWrt:/#

Every single process has a different virtual address space, you don't have a global "userspace" virtual address space.每个进程都有不同的虚拟地址空间,您没有全局“用户空间”虚拟地址空间。 What VMSPLIT_1G does is simply limit the range of virtual addresses available to userspace programs. VMSPLIT_1G所做的只是限制用户空间程序可用的虚拟地址范围。 You can very well have multiple programs where the entire 1G of available virtual address space is mapped, but still have memory available because the pages are not actually resident in main memory.您可以有多个程序,其中映射了整个 1G 可用虚拟地址空间,但仍然有可用内存,因为页面实际上并不驻留在主内存中。

If you want to take a look at how much memory is currently in use, then /proc/meminfo is what you are looking for, but keep in mind that you have to check it when your program is running, not before/after running it, as that would be pointless.如果您想查看当前正在使用多少内存,那么/proc/meminfo就是您要查找的内容,但请记住,您必须在程序运行时检查它,而不是在运行之前/之后,因为那将毫无意义。 As per individual processes, you have /proc/[PID]/stat and /proc/[PID]/status which both provide the "resident set size" (amount of used virtual memory memory actually resident in main memory), or you can take a look at /proc/[PID]/maps to inspect the virtual addresses used by a specific process.根据各个进程,您有/proc/[PID]/stat/proc/[PID]/status两者都提供“常驻集大小”(实际驻留在主内存中的已用虚拟内存量),或者您可以查看/proc/[PID]/maps以检查特定进程使用的虚拟地址。

Now if i run stressapptest in userspace with 80% of the available memory shown using cat /proc/meminfo , it throws an error现在,如果我使用 cat /proc/meminfo显示的可用内存的 80% 在用户空间中运行 stressapptest,它会抛出错误

This seems to be expected in your configuration, it may happen for different reasons, the most likely one is that the program is trying to request a single block of memory that is too large, and the memory allocator used by the program (ie Glibc's malloc ) cannot allocate one, so it returns failure even if technically there still is some free memory to use.这在您的配置中似乎是预料之中的,它可能由于不同的原因而发生,最可能的一个是程序试图请求过大的单个内存块,以及程序使用的内存分配器(即 Glibc 的malloc ) 无法分配一个,因此即使从技术上讲仍有一些可用内存可供使用,它也会返回失败。

For example, even if you have a lot of free main memory, but your process has a virtual address which is almost full, and you try to do a single mmap with a size that is too large, the kernel will not be able to fit those many pages in a contiguous block of virtual memory and will return failure.例如,即使您有很多空闲的主内存,但是您的进程的虚拟地址几乎已满,并且您尝试执行大小过大的单个mmap ,内核将无法容纳虚拟内存的连续块中的许多页面并将返回失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM