将一个C程序的堆栈不断缩水? [英] Will the stack of a C program ever shrink?

查看:109
本文介绍了将一个C程序的堆栈不断缩水?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,每个正在运行的C程序都有(128K我的机器上)被称为[堆栈]私人映射,它最初是相当小的,但将增长,以适应任何自动变量(最多堆栈大小限制)。我认为这是我的程序的调用堆栈的位置。

I've noticed that every running C program has a private mapping called [stack] that is initially quite small (128k on my machine), but will grow to accomodate any automatic variables (up to the stack size limit). I assume this is where the call stack of my program is located.

但是,它似乎没有以往任何时候都退缩到原来的大小。有什么办法来释放内存,而不终止该进程?

However, it doesn't seem to ever shrink back to its original size. Is there any way to free up that memory without terminating the process?

如何C栈内部实现的;什么增加了需求[堆栈]映射的大小?一些编译器生成code,C库或操作系统?在哪里增加触发?

How is the C stack implemented internally; what increases the size of the [stack] mapping on demand? Some compiler generated code, the C library or the operating system? Where is the increase triggered?

更新的:我使用Linux 3.0.0,GCC 4.6.1和glibc6上X86-64;因为这可能是$ P $具体实施ptty,它如何工作的任何信息存在就可以了。

Update: I'm using Linux 3.0.0, gcc 4.6.1 and glibc6 on x86-64; as this is probably pretty implementation specific, any information on how it works there would be fine.

推荐答案

在Linux的/ MMU(中!MMU你不能成长栈),堆栈是生长在页面故障处理程序。对于x86,是否成长堆栈由以下code从决定弓/ 86 / MM / fault.c:do_page_fault()

In Linux/MMU (in !MMU you cannot grow the stack), the stack is grown in the page fault handler. For x86, whether to grow the stack is decided by the following code from arch/x86/mm/fault.c:do_page_fault():

        if (error_code & PF_USER) {
            /*
             * Accessing the stack below %sp is always a bug.
             * The large cushion allows instructions like enter
             * and pusha to work. ("enter $65535, $31" pushes
             * 32 pointers and then decrements %sp by 65535.)
             */
            if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
                    bad_area(regs, error_code, address);
                    return;
            }
    }
    if (unlikely(expand_stack(vma, address))) {
            bad_area(regs, error_code, address);
            return;
    }

expand_stack()检查通常RLIMITS(RLIMIT_AS,RLIMIT_STACK,RLIMIT_MEMLOCK),手中LSM是否允许增长栈,是否有太多的过量使用,等等。 ,最后生长堆栈

expand_stack() checks the usual RLIMITS (RLIMIT_AS, RLIMIT_STACK, RLIMIT_MEMLOCK), whether LSMs will allow to grow the stack, whether there's too much overcommit, etc..., and finally grows the stack.

这篇关于将一个C程序的堆栈不断缩水?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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