简体   繁体   English

如何在 linux/ubuntu 中找到进程使用的确切堆栈和堆内存?

[英]How to find exact stack and heap memory used by a process in linux/ubuntu?

I have project written in C-language.我有用 C 语言编写的项目。 I need to find out how much Stack(local variables,..) and Heap memory(allocated with malloc) this process is using.我需要找出这个进程使用了多少堆栈(局部变量,..)和堆内存(使用 malloc 分配)。 So that I can make a decision that whether a particular Microcontroller(currently my controller has 30KB RAM) meet my project's minimum RAM/Stack/Heap requirements or not.这样我就可以决定某个特定的微控制器(目前我的控制器有 30KB RAM)是否满足我项目的最低 RAM/堆栈/堆要求。

I tried /proc/pid/smaps.我试过/proc/pid/smaps。 But it is showing minimun 4kB stack even if the file contains only 2 local integer variables.(I think it's showing Page size or memory range).但即使文件仅包含 2 个局部整数变量,它也会显示最小 4kB 堆栈。(我认为它显示的是页面大小或内存范围)。

top command output is not useful for this requirement. top 命令输出对此要求没有用。

Is there any tool to find out stack(with moderate accuracy in bytes) used by a process in realtime in the form of variables etc(or atleast maximum value reached in lifetime also fine).(with this later I need to setup CI job for finding these.)是否有任何工具可以实时以变量等形式找出进程使用的堆栈(以字节为单位精度适中)(或者至少在生命周期内达到的最大值也可以)。(稍后我需要设置 CI 作业找到这些。)

Atleast I could find out heap using malloc wrapper API like below.(don't know how to find out deallocated memory in a easy way.)至少我可以使用如下所示的 malloc 包装器 API 找出堆。(不知道如何以简单的方式找出已释放的内存。)

Eg: void call_malloc(size_t n) { usedMem = usedMem + n;例如:void call_malloc(size_t n) { usedMem = usedMem + n; // global variable p= malloc(n); // 全局变量 p= malloc(n); } }

I found reasonable solution.我找到了合理的解决方案。

While compiling use -fstack-usage flag. Eg: gcc -g -fstack-usage filename.c编译时使用-fstack-usage flag. Eg: gcc -g -fstack-usage filename.c -fstack-usage flag. Eg: gcc -g -fstack-usage filename.c

Use the same in CFLAGS in makefile.在 makefile 的 CFLAGS 中使用相同的内容。 No need to run the executable.无需运行可执行文件。 After compiling, the same file name with.su extension will be there in that folder.编译后,该文件夹中将出现相同名称的扩展名为.su 的文件。 It can be opened using cat/vim/notepad etc.可以使用cat/vim/notepad等打开。

For heap memory calculation, simply use valgrind.对于堆内存计算,只需使用valgrind。

PS: While digging more I found below answer. PS:在挖掘更多内容时,我发现了以下答案。 How to determine maximum stack usage in embedded system with gcc? 如何使用 gcc 确定嵌入式系统中的最大堆栈使用量?

If you run your code with the very basic command如果您使用非常基本的命令运行代码

/usr/bin/time --verbose ${executable}

you will get the following type of output.您将获得以下类型的输出。 If you focus on the " Maximum resident set size ", and consider the values for " Average stack size " and " Average total size " (ie stack + heap), would that address your needs?如果您专注于“最大驻留集大小”,并考虑“平均堆栈大小”和“平均总大小”(即堆栈 + 堆)的值,这会满足您的需求吗?

Command being timed: "{your_executable}"
User time (seconds): 0.00
System time (seconds): 0.01
Percent of CPU this job got: 90%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.01
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 4032
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 385
Voluntary context switches: 5
Involuntary context switches: 84
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0

This is also discussed more expansively here .这也在此处进行了更广泛的讨论。

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

相关问题 在Linux中获取每个进程的堆和堆栈的大小 - Get the size of heap and stack per process in Linux 是否有设置进程堆栈/堆内存分配的功能? - Is there a function to set a process stack/heap memory allocation? 堆栈、堆中的内存是如何排列的? - How is memory arranged in stack, heap? Linux中进程使用的内存页面 - Memory pages used by a process in Linux 如何在Linux中的相同进程下为线程分配堆栈或内存 - How Stack or memory is allocated for threads under the same process in Linux Linux如何知道进程使用了​​多少物理内存? - How does Linux know how much physical memory is used by a process? 对于 C 程序过程,我如何知道 memory 中数据、堆栈和堆的起始地址及其大小? - How can I know the starting address of data,stack and heap, and their sizes in memory for a C program process? 是否有可能通过 valgrind 准确地找到一个进程消耗了多少堆栈内存? - Is it possible to exactly find how much stack memory is consumed by a process by valgrind? 如何在Linux上使用C检查堆栈和堆使用情况? - How to check stack and heap usage with C on Linux? 如何使用C / C ++系统调用获取Linux中进程的堆内存的当前大小? - How do I get the current size of the heap memory of a process in Linux using C/C++ system calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM