简体   繁体   English

如何计算程序的内存大小?

[英]How to calculate the memory size of a program?

Lets say I have ac program where I use only stack variables, no dynamic variables (malloc, ...) 可以说我有一个ac程序,其中只使用堆栈变量,不使用动态变量(malloc,...)

Is it possible to calculate how much memory my program will take during run time? 是否可以计算出我的程序在运行时需要占用多少内存?

It not only depends on the OS and compiler like stated here. 它不仅取决于此处所述的OS和编译器。

It is simply impossible to determine it in advance in general. 通常根本不可能预先确定它。

If you make use of recursion calls, for example, you would have to know the parameters of these calls and it ends up in very complicated calculations that are impossible to make in advance if some parameters rely on user input or other unpredictable things. 例如,如果您使用递归调用,您将必须知道这些调用的参数,并且最终会产生非常复杂的计算,如果某些参数依赖于用户输入或其他不可预测的事情,则无法提前进行计算。

Of course you could make some worst case scenario but generally the upper bounds are unlimited for most problems. 当然,您可能会遇到一些最坏的情况,但是通常大多数问题的上限是无限的。

So only if your program is very simple and linear you can do so. 因此,仅当您的程序非常简单且线性时,您才可以这样做。

有可用的CPU和内存分析工具,可帮助您查找程序在运行时需要多少CPU时间或内存。

Yes, but the method of doing so depends on the OS. 是的,但是方法取决于操作系统。 What platform are you targeting? 您定位到哪个平台?

Yes - According to your compiler/os each object you put on the stack has a size (int - 4 bytes for example but it defers from compiler to compiler and from os to os) - you can use sizeof to find in runtime the size of what you put on the stack. 是的-根据您的编译器/操作系统,放置在堆栈上的每个对象都有一个大小(例如int-4字节,但它在编译器之间以及从os到os有所不同)-您可以在运行时使用sizeof来找到您放在堆栈上的内容。 Eventually the szie of the program will be the size of the code + the size of the stack you create. 最终,程序的大小将是代码的大小+您创建的堆栈的大小。 (finding the size of the code is harder, but you can just load the program and see in task manager how much it takes, this should give you some estimation if you're on window). (发现代码的大小比较困难,但是您可以只加载程序并在任务管理器中查看需要多少,如果您在窗口中,这应该可以给您一些估计)。 I think the top will do it on linux. 我认为顶层将在Linux上完成。

If Linux such as FC9 etc, please see /proc/[PID]/maps, eg: 如果是Linux,例如FC9等,请参阅/ proc / [PID] / maps,例如:

cat /proc/2738/maps

that'll make sense 那会很有意义

It also depends on if how deep your call trace is. 它还取决于您的呼叫跟踪的深度。 (Are you using recursive functions or not and so on.) Also, can your OS grow stacks? (您是否使用递归函数等等。)此外,您的OS可以增加堆栈吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM