简体   繁体   English

Linux上的虚拟内存大小

[英]Virtual Memory size on Linux

I'm trying to understand in depth the virtual memory management on OS Linux. 我试图深入理解OS Linux上的虚拟内存管理。

I don't really understand how the OS determine the size of the VM for a process. 我真的不明白操作系统如何确定进程的VM大小。

I know that an 32-bits x86 OS can give up to 3 GB of vm adress space... Is it always true ? 我知道32位x86操作系统可以提供高达3 GB的vm地址空间......它总是如此吗?

In my case, i have about 110 MB physical memory with and 32-bits Linux, and my main process have a vm adress space about 660 MB. 在我的情况下,我有大约110 MB物理内存和32位Linux,我的主进程有一个约660 MB的虚拟机地址空间。 However, only 50 MB are in physical memory (the RSS of my process), so my physical RAM isn't full. 但是,物理内存中只有50 MB(我的进程的RSS),因此我的物理内存不足。 The rest is free and almost the whole is used by page cache. 其余的是免费的,几乎整个页面缓存使用。 That's seems to be a normal behaviour. 这似乎是一种正常行为。

If i check /proc/my_process_PID/smap, there are several 8 MB anonymous VMA. 如果我检查/ proc / my_process_PID / smap,则有几个8 MB的匿名VMA。

My actual problem is that i need to make a additionnal 10 MB malloc in the code, but unfortunately OOM-Killer kills my process (out-of-memory) ... I think there are no more free available pages in the vm for the Heap, isn't it ? 我的实际问题是我需要在代码中创建一个额外的10 MB malloc,但不幸的是OOM-Killer杀死了我的进程(内存不足)......我认为vm中没有更多的免费可用页面堆,不是吗? Is there an huge memory leak somewhere ? 某处是否存在巨大的内存泄漏?

Why the OS doesn't extend my process vm size so ? 为什么操作系统不会扩展我的进程vm大小呢?

For information the vm size is unlimited : ulimit -v : unlimited 有关信息,vm大小是无限的:ulimit -v:无限制

You can have 3GB of virtual memory per process (approximately, on many 32-bit Linux), and keep on creating new processes taking up gigabytes upon gigabytes of virtual memory. 每个进程可以拥有3GB的虚拟内存(大约在许多32位Linux上),并继续创建占用千兆字节虚拟内存的新进程。 There's a small overhead in the kernel, but virtual memory is very cheap. 内核中的开销很小,但虚拟内存非常便宜。 The amount of address space you're using is probably not important, and it probably won't trigger the OOM killer. 您正在使用的地址空间量可能并不重要,它可能不会触发OOM杀手。

However, your system only has so much RAM. 但是,您的系统只有很多RAM。 As you start using pages in your address space (writing to them), the kernel is forced to find physical RAM to map them to. 当您开始在地址空间中使用页面(写入它们)时,内核将被迫查找物理RAM以将其映射到。 If there's no physical RAM, the kernel can evict other pages from RAM -- either swap them out or discard them. 如果没有物理RAM,内核可以从RAM中驱逐其他页面 - 将它们交换出来或丢弃它们。 But if it can't evict any pages, then it triggers the OOM killer. 但如果它不能驱逐任何页面,那么它会触发OOM杀手。

Running out of address space will cause malloc to return NULL on my system, instead of triggering the OOM killer. 耗尽地址空间将导致malloc在我的系统上返回NULL ,而不是触发OOM杀手。

It sounds like your process is simply using too much RAM. 听起来你的过程只是使用了太多的RAM。 RSS isn't the amount of memory your process uses, it's just the amount that's in physical RAM right now . RSS是不是内存量的过程中使用,它只是现在在物理内存量。 If your process has a memory leak and keeps growing, RSS will eventually stop growing -- because for every new page you use, the kernel will evict one page from your process. 如果您的进程有内存泄漏并且不断增长,RSS最终会停止增长 - 因为对于您使用的每个新页面,内核将从您的进程中逐出一页。

Try using a memory profiler, like Valgrind . 尝试使用内存分析器,如Valgrind This will help you sort out what memory you should be worried about (mallocs) and what memory you can ignore (shared libraries and other memory mapped files). 这将帮助您理清您应该担心的内存(mallocs)以及您可以忽略的内存(共享库和其他内存映射文件)。 The kernel (and /proc) won't give you enough detail. 内核(和/ proc)不会给你足够的细节。

The total amount of virtual memory space available on a linux system is (roughly) RAM + swap space - kernel overhead . Linux系统上可用的虚拟内存空间总量(大致)是RAM + swap space - kernel overhead The RAM is the hardware you have installed and the kernel overhead is roughly a constant (varies between kernel versions though), so the only easy way to control the total VM space available is by adding or removing swap space. RAM是您安装的硬件,内核开销大致是一个常数(尽管内核版本不同),因此控制可用VM总空间的唯一简便方法是添加或删除交换空间。

In addition to the total limit, there's also a per-process VM limit. 除了总限制之外,还有每个进程的VM限制。 This is configurable and (on 32bit linux at least) is at most 3GB, but may be quite a bit less. 这是可配置的(至少在32位linux上)最多为3GB,但可能要少得多。 ulimit -v will tell you this limit or can be used to change it. ulimit -v会告诉你这个限制或者可以用它来改变它。

When a process requests more VM space (generally via malloc), the kernel will look at all of these limits, and if any of them would be exceeded, will return 0. The OOM killer, on the other hand, only kicks in when you're getting close to the total VM limit. 当进程请求更多的VM空间(通常通过malloc)时,内核将查看所有这些限制,如果超过其中任何一个,将返回0.另一方面,OOM杀手只会在你启动时启动'越来越接近VM的总限制。 However, when the OOM killer kills you, you just die -- there's no out-of-memory error or any opportunity to catch it. 然而,当OOM杀手杀死你时,你就死了 - 没有内存错误或任何抓住它的机会。

So if you really are running into the total VM limit, and want to avoid that, you can either allocate more swap space, or get rid of (kill or not start in the first place) other processes that are using a lot of VM space so as to free up some for your program. 因此,如果您真的遇到了VM的总限制,并希望避免这种情况,您可以分配更多的交换空间,或者除了(首先杀死或不启动)其他使用大量VM空间的进程以便为你的程序腾出一些东西。

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

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