How to check heap size for a process on Linux

后端 未结 5 689
清歌不尽
清歌不尽 2020-12-01 08:24

I was writing some code and it kept crashing. Later after digging the dumps I realized I was overshooting the maximum heap limit (life would have been easier if I had added

5条回答
  •  -上瘾入骨i
    2020-12-01 09:10

    I'd like to add one point to the previous answers.

    Apps have the illusion that malloc() returns 'solid' blocks; in reality, a buffer may exist scattered, pulverized, on many pages of RAM. The crucial fact here is this: the Virtual Memory of a process, containing its code or containing something as a large array, must be contiguous. Let's even admit that code and data be separated; a large array, char str[universe_size], must be contiguous.

    Now: can a single app enlarge the heap arbitrarily, to alloc such an array?

    The answer could be 'yes' if there were nothing else running in the machine. The heap can be ridiculously huge, but it must have boundaries. At some point, calls to sbrk() (in Linux, the function that, in short, 'enlarges' the heap) should stumble on the area reserved for another application.

    This link provides some interesting and clarifying examples, check it out. I did not find the info on Linux.

提交回复
热议问题