Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 292
shim_mang Avatar asked Oct 19 '25 15:10

shim_mang


2 Answers

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.

like image 64
Andrew Henle Avatar answered Oct 22 '25 04:10

Andrew Henle


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

     /proc/<pid>/maps   
like image 44
Vijay S B Avatar answered Oct 22 '25 05:10

Vijay S B