Get the size of heap and stack per process in Linux

巧了我就是萌 提交于 2020-01-05 05:43:05

问题


I wanted to know the size of heap and stack per process in linux. Is there any way to find it?

I found out that sbrk(0) will give me the end of heap. But how can I find the start of heap to get the heap size?

Also on stack size is there any way to find the start of stack and current stack pointer address per process through any library calls or system calls?


回答1:


On Linux, you can read /proc/[PID]/maps and find [heap] and [stack] entries.

But for the GLIBC heap implementations usually used on Linux, the "heap" consists of both memory obtained via sbrk() that shows up in the /proc/[PID]/maps file as [heap] and memory obtained via mmap() - see this quesiton. So the "size" of the heap is going to be very hard to determine with certainty.

And the region labelled [stack] in the maps file is the stack for the main thread only. Multithreaded processes will have multiple stacks, one for each thread. And they will show up in the maps file as anonymous memory - maybe. The application can control the memory used for a thread's stack via the use of pthread_attr_setstack() and set it to any memory the application might use.




回答2:


You can get in the below file. You should be root user.

     /proc/<pid>/maps   


来源:https://stackoverflow.com/questions/40127980/get-the-size-of-heap-and-stack-per-process-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!