简体   繁体   English

在Linux中获取每个进程的堆和堆栈的大小

[英]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. 我想知道linux中每个进程的堆和堆栈的大小。 Is there any way to find it? 有没有办法找到它?

I found out that sbrk(0) will give me the end of heap. 我发现sbrk(0)会给我堆的结束。 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. 在Linux上,您可以读取/proc/[PID]/maps并查找[heap][stack]条目。

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 . 但对于通常在Linux上使用的GLIBC堆实现,“堆”包括通过sbrk()获得的内存,它在/proc/[PID]/maps文件中显示为[heap] 通过mmap()获得的内存 -看到这个问题 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. 并且maps文件中标记为[stack]的区域仅是主线程的堆栈。 Multithreaded processes will have multiple stacks, one for each thread. 多线程进程将有多个堆栈,每个线程一个堆栈。 And they will show up in the maps file as anonymous memory - maybe. 它们将作为匿名内存显示在maps文件中 - 也许。 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. 应用程序可以通过使用pthread_attr_setstack()来控制用于线程堆栈的内存,并将其设置为应用程序可能使用的任何内存。

You can get in the below file. 你可以进入下面的文件。 You should be root user. 你应该是root用户。

     /proc/<pid>/maps   

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

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