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

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

问题描述

我在写一些code和它保持崩溃。挖转储后,后来我意识到我是超调的最大堆限制(生活本来就容易,如果我加在的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 命令电流限值,并寻求这条线最大内存大小(千字节,-m)3008828 ,此行我了OpenSuse 11.4 x86_64的用〜公羊3.5吉布说,我有每个进程的RAM大约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\n",maxMemMiB);
                        memset(memPointer,0,maxMemMiB);
                        free(memPointer);
                }
                maxMemMiB+=oneHundredMiB;
                memPointer=malloc(maxMemMiB);
        }while(memPointer != NULL);
        printf("Max Usable Memory aprox = %zi\n",maxMemMiB-oneHundredMiB);
        return 0;
}

该计划得到内存100MiB增量,presents当前分配的内存,分配就可以了0,然后释放内存。当系统无法给出更多的内存,返回NULL并显示RAM的最终使用的最大金额。

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.

对于交换的RAM和400MiB 256 MIB对Ubuntu的10.04x86 VM比较的ulimit报告是内存大小(千字节,-m)无限和我的小程序报道524.288.000字节,这大概是组合形式的RAM和交换,扣除由其他软件和内核使用的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.

编辑:正如亚当Zalcman写道,的ulimit -m 不再荣幸在新的2.6及以上版本的Linux内核,所以我认错。但的ulimit -v 很荣幸。对于实际的结果,你应该-v代替-m,并查找虚拟内存(​​千字节,-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

长total_available_ram =的sysconf(_SC_AVPHYS_PAGES)*的sysconf(_SC_PAGESIZE);

这将排除缓存和缓冲存储器,因此这个数字可能远远大于实际可用内存小。 OS缓存可以安静的大和驱逐可以给需要额外的内存,但是这是由内核来处理。

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天全站免登陆