Get the size of heap and stack per process in Linu

2019-07-23 15:50发布

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?

标签: c linux stack heap
2条回答
爷、活的狠高调
2楼-- · 2019-07-23 16:18

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.

查看更多
神经病院院长
3楼-- · 2019-07-23 16:23

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

     /proc/<pid>/maps   
查看更多
登录 后发表回答