如何测量全局变量的总大小?
我正在创建一个 ac 程序,打算在不久的时间内在 ARM 处理器上运行。我想测量全局变量使用的内存量,同时忽略堆栈/堆的大小。有没有办法让 gcc 在编译时转储此信息或从编译的二进制文件中检索此信息?
I'm creating a c program that I intend to run on an ARM processor in the near timeframe. I want to measure the amount of memory I'm using with my global variables while ignoring the size of the stack/heap. Is there a way to either get gcc to dump this out at compile time or to retrieve this information from the compiled binary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看内存去向的一个好方法是查看链接器图。链接器映射是由链接器生成的文件,详细说明了程序的所有内存位置。您可以逐个符号地查看全局变量和代码的内存分配。我过去曾在内存要求严格的项目中使用过链接器映射。它们可以轻松识别问题区域,例如占用大量空间的全局内存缓冲区。
将此选项添加到 gcc 命令行以生成链接器映射:
-Wl,-Map=output.map
A great way to see where your memory is going is to look at the linker map. A linker map is a file that is generated by the linker and details all memory locations for the program. You can see memory allocation on a symbol by symbol basis for global variables as well as code. I've used linker maps in the past for projects which have tight memory requirements. They make it easy to identify problems areas like global memory buffers which take up alot of space.
Add this option to the gcc command line to generate the linker map:
-Wl,-Map=output.map
GNU binutils 套件包含一个名为“size”的程序,这是获取所需数据(或者至少是合理的近似值)的最简单方法。对于典型的程序(在这种情况下,不是一个小的嵌入式程序),输出可能如下所示:
前三列是二进制文件中各部分的大小:“文本”是可执行代码,“数据”是常量,等等第四——包括那些用显式初始化器表示初始变量的变量——而“bss”是所有隐式静态初始化的初始化器。在典型的嵌入式程序中,这些静态初始化程序几乎专门用于全局变量(并且,出于您的目的,您可能希望在测量中包含其他静态变量,因为它们也不在堆栈或堆上)。
因此,我认为你最终得到的“data”和“bss”的总和本质上就是你想要的。 (读完 hlovdal 链接的文章后,我对此不太确定;也许评论者可以添加确认?)
(之后,“dec”和“hex”是十进制和十六进制所有内容的总大小,并且“文件名”当然是显而易见的。)
The GNU binutils suite contains a program called "size", which is the easiest way to get the data you need -- or, at least, a reasonable approximation. For a typical program (in this case, not a small embedded one), the output might look like:
The first three columns are sizes of the sections in the binary: "text" is the executable code, "data" is constants and so forth -- including ones that represent initial variables with explicit initializers -- and "bss" is initializers for everything that gets implicitly statically initialized. In a typical embedded program, those static initializers are pretty much exclusively for global variables (and, for your purposes, you might want to include the other static variables in your measurement anyway since they're also not on the stack or heap).
Thus, I think that you end up with the sum of "data" and "bss" being essentially what you want. (After reading the article hlovdal linked to, I'm less sure of this than I was; perhaps commenters can add confirmation?)
(After that, "dec" and "hex" are the total size of everything in decimal and hexadecimal, and "filename" is of course obvious.)
您需要使用 objdump 分析不同的内存段。请参阅此处获取文章和此处获取另一个答案其中提供了一些细节。
You need to analyse the different memory segments with
objdump
. See here for an article and here for another answer which gives some details on this.