如何确定Linux中程序的堆栈大小? [英] How to determine Stack size of a Program in linux?

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

问题描述

如何确定linux中程序的当前堆栈大小?

How does one determine the current stack size of a program in linux?

据说在Linux中每个程序的堆栈大小为8 MB,但是当您使用 cat/proc//mmap会显示不同的大小.

it is said that the stack size of each program will be 8 MB in linux but when you use cat /proc//mmap it shows a different size.

此外,如何确定相关线程的堆栈大小?既然有人说线程有自己的私有堆栈?

Also, how does one determine stack size of associated threads? Since it is said that threads have their own private stack?

推荐答案

如果只需要当前的堆栈大小,则可以在main()的顶部声明一个变量,获取其地址,然后将其与以下地址进行比较:在定义当前"的位置处声明的变量.差异应该是堆栈已增长的大致大小.

If you simply want the current stack size, you could declare a variable at the top of main(), take its address, and compare it to the address of a variable declared at wherever you define "current" to be. The difference should be the approximate size that the stack has grown.

如果您想知道为堆栈保留了多少内存,可以检查/proc/[pid]/maps,它的区域标记为[stack].例如,我的atd流程有:

If you want to know how much memory is reserved for the stack, you can check /proc/[pid]/maps, which has a region marked as [stack]. For example, my atd process has:

7fff72a41000-7fff72a56000 rw-p 00000000 00:00 0                          [stack]
0175b000-0177c000 rw-p 00000000 00:00 0                                  [heap]

这给你一个主意.

当我想知道程序使用的最大堆栈大小时,一个朋友与我分享的一个巧妙技巧,如下所示.如果有人觉得有用,我会在这里展示:)

A neat trick that a friend shared with me when I wanted to know the maximum size of stack that my program used was as follows. I'll present it here in case someone finds it useful :)

1)在main()开头附近的函数中,使用alloca()或一个很长的数组在希望使用的尽可能多的堆栈上涂抹0xDEADBEEF或其他一些不太可能的常量.当小函数返回时,该内存将被释放".

1) In a function called near the beginning of main(), use alloca() or a very long array to scribble 0xDEADBEEF or some other such unlikely constant over as much of the stack as you expect could be used. This memory will be "freed" when the small function returns.

2)在main的末尾,再次使用alloca()来获取内存区域,并在其中搜索"您曾经用来涂抹的任何魔术常数(您可能会尝试找到其中的第一个块)或跳过某些可能已经分配但从未使用过的内存区域),并且该指针到达的位置指示您的最大堆栈使用率.

2) At the end of main, again use alloca() to grab a region of memory and "search" down through it for whatever magic constant you used to scribble (you might try to find the first block of 64 of them or something to skip over regions of memory that may have been allocated but simply never used), and where that pointer lands indicates your maximum stack usage.

并不完美,但这对我的工作很有用!

Not perfect, but it was useful for what I was doing!

这篇关于如何确定Linux中程序的堆栈大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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