如何在 Linux 上检查进程的堆大小 [英] How to check heap size for a process on Linux

查看:29
本文介绍了如何在 Linux 上检查进程的堆大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,但它一直崩溃.后来在挖掘转储之后,我意识到我超出了最大堆限制(如果我在 malloc 上添加了一个检查,生活会更容易).虽然我解决了这个问题,但有什么办法可以增加我的堆大小吗?

I was writing some code and it kept crashing. Later after digging the dumps I realized I was overshooting the maximum heap limit (life would have been easier if I had added a check on malloc). Although I fixed that, is there any way to increase my heap size?

PS:这里有一个很类似的问题但我的回答不清楚.

PS: A quite similar question here but the reply is unclear to me.

推荐答案

堆通常与架构上的可寻址虚拟内存一样大.

The heap usually is as large as the addressable virtual memory on your architecture.

您应该使用 ulimit -a 命令检查系统当前限制并在我的 OpenSuse 上查找此行 max memory size (kbytes, -m) 300882811.4 x86_64 和 ~3.5 GiB 内存表示我每个进程大约有 3GB 内存.

You should check your systems current limits with the ulimit -a command and seek this line max memory size (kbytes, -m) 3008828, this line on my OpenSuse 11.4 x86_64 with ~3.5 GiB of ram says I have roughly 3GB of ram per process.

然后您就可以使用这个简单的程序来真正测试您的系统,以检查每个进程的最大可用内存:

Then you can truly test your system using this simple program to check max usable memory per process:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char* argv[]){
        size_t oneHundredMiB=100*1048576;
        size_t maxMemMiB=0;
        void *memPointer = NULL;
        do{
                if(memPointer != NULL){
                        printf("Max Tested Memory = %zi
",maxMemMiB);
                        memset(memPointer,0,maxMemMiB);
                        free(memPointer);
                }
                maxMemMiB+=oneHundredMiB;
                memPointer=malloc(maxMemMiB);
        }while(memPointer != NULL);
        printf("Max Usable Memory aprox = %zi
",maxMemMiB-oneHundredMiB);
        return 0;
}

这个程序以 100MiB 的增量获取内存,呈现当前分配的内存,在其上分配 0,然后释放内存.当系统不能提供更多内存时,返回NULL并显示最终的最大可用内存量.

This programs gets memory on 100MiB increments, presents the currently allocated memory, allocates 0's on it,then frees the memory. When the system can't give more memory, returns NULL and it displays the final max usable amount of ram.

需要注意的是,您的系统将在最后阶段开始大量交换内存.根据您的系统配置,内核可能会决定终止某些进程.我使用 100 MiB 增量,因此某些应用程序和系统有一些喘息空间.你应该关闭任何你不想崩溃的东西.

The Caveat is that your system will start to heavily swap memory in the final stages. Depending on your system configuration, the kernel might decide to kill some processes. I use a 100 MiB increments so there is some breathing space for some apps and the system. You should close anything that you don't want crashing.

话虽如此.在我写这篇文章的系统中,没有任何崩溃.并且上面的程序报告与 ulimit -a 几乎不一样.不同的是,它实际测试了内存,并通过memset() 确认内存已给出并使用.

That being said. In my system where I'm writing this nothing crashed. And the program above reports barely the same as ulimit -a. The difference is that it actually tested the memory and by means of memset() confirmed the memory was given and used.

为了在具有 256 MiB 内存和 400MiB 交换空间的 Ubuntu 10.04x86 VM 上进行比较,ulimit 报告是内存大小(kbytes,-m)无限制,而我的小程序报告了 524.288.000 字节,大致是ram和swap的组合,不包括其他软件和内核使用的ram.

For comparison on a Ubuntu 10.04x86 VM with 256 MiB of ram and 400MiB of swap the ulimit report was memory size (kbytes, -m) unlimited and my little program reported 524.288.000 bytes, which is roughly the combined ram and swap, discounting ram used by others software and the kernel.

正如 Adam Zalcman 所写的那样,ulimit -m 在较新的 2.6 及更高版本的 linux 内核上不再受尊重,所以我已经更正了.但是 ulimit -v 很荣幸.对于实际结果,您应该将 -m 替换为 -v,并查找 virtual memory (kbytes, -v) 4515440.我的 suse 盒子的 -m 值与我的小实用程序报告的值一致,这似乎只是偶然.请记住,这是内核分配的虚拟内存,如果物理内存不足,将需要交换空间来弥补.

As Adam Zalcman wrote, ulimit -m is no longer honored on newer 2.6 and up linux kernels, so i stand corrected. But ulimit -v is honored. For practical results you should replace -m with -v, and look for virtual memory (kbytes, -v) 4515440. It seems mere chance that my suse box had the -m value coinciding with what my little utility reported. You should remember that this is virtual memory assigned by the kernel, if physical ram is insufficient it will take swap space to make up for it.

如果你想在不干扰任何进程或系统的情况下知道有多少物理内存可用,你可以使用

If you want to know how much physical ram is available without disturbing any process or the system, you can use

long total_available_ram =sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE);

这将排除缓存和缓冲内存,因此该数字可能远小于实际可用内存.操作系统缓存可能非常大,它们的驱逐可以提供所需的额外内存,但这是由内核处理的.

this will exclude cache and buffer memory, so this number can be far smaller than the actual available memory. OS caches can be quiet large and their eviction can give the needed extra memory, but that is handled by the kernel.

这篇关于如何在 Linux 上检查进程的堆大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆