简体   繁体   中英

Get the size of heap and stack per process in Linux

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?

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.

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

     /proc/<pid>/maps   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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