堆栈内存消耗是如何计算的? [英] How is stack memory consumption calculated?

查看:35
本文介绍了堆栈内存消耗是如何计算的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算我的程序的堆栈内存消耗.gcc 的 -fstack-usage 只计算函数的堆栈使用情况,但据我所知不包括该函数中的额外函数调用.

I need to calculate the stack memory consumption of my program. gcc's -fstack-usage only calculates the stack usage of function, but does not include an additional function call in that function as far as I understand.

void test1(){
    uint32_t stackmemory[100];
    function1();                    //needs aditional stack, say 200 Bytes
    uint32_t stackmemory2[100];
}

void test2(){
    uint32_t stackmemory[100];
    uint32_t stackmemory2[100];
    function1();                   //needs additional stack, say 200 Bytes
}

哪个 test() 函数使用更少的堆栈?我会说 test1(),因为在 function1() 调用之后堆栈被释放.还是这取决于优化级别 -Os/-O2...?

Which test() function uses less stack? I would say test1(), as the stack is freed after the function1() call. Or does this depend on the optimization level -Os/-O2...?

一旦进入函数,编译器是否会在 test1() 中为其所有静态变量分配内存?还是在到达该行时分配了 stackmemory2[100] ?

Does the compiler allocate memory in test1() for all its static variables, as soon as the function is entered? Or is stackmemory2[100] allocated when the line is reached?

推荐答案

一般来说,你需要结合调用图信息和 -fstack-usage 生成的 .su 文件来找到最深的堆栈使用从特定功能开始.从 main() 或线程入口点开始,将为您提供该线程的最坏情况.

In general you need to combine call-graph information with the .su files generated by -fstack-usage to find the deepest stack usage starting from a specific function. Starting at main() or a thread entry-point will then give you the worst-case usage for that thread.

如所讨论的那样,已经为您完成了创建此类工具的工作,这很有帮助 此处,使用来自此处的 Perl 脚本.

Helpfully the work to create such a tool has been done for you as discussed here, using a Perl script from here.

ARM 的 armcc 编译器(在 Keil ARM-MDK 中使用)具有此功能内置 并且可以在链接图中包含详细的堆栈分析,包括最坏情况的调用路径和非确定性堆栈使用的警告(例如由于递归).

ARM's armcc compiler (as used in Keil ARM-MDK) has this functionality built-in and can include detailed stack analysis in the link map, including the worst-case call path and warnings of non-deterministic stack usage (due to recursion for example).

根据我观察多个编译器行为的经验,通常为函数的生命周期定义堆栈帧,而不管声明的变量的生命周期和范围如何.所以这种情况下的两个版本不会有什么不同.不声明它们 volatile 优化器可能会在任何情况下删除这两个数组.但是,您不应依赖在这方面的任何观察都是通用的 - 它是实现而不是语言定义.

In my experience observing the behaviour of several compilers, the stack-frame is typically defined for the lifetime of the function regardless of the lifetime and scope of the variables declared. So the two versions in that case will not differ. Without declaring them volatile the optimiser will likely remove both arrays in any event. However you should not rely on any observations in this respect being universal - it implementation rather then language defined.

这篇关于堆栈内存消耗是如何计算的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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