How to check heap size for a process on Linux

后端 未结 5 701
清歌不尽
清歌不尽 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条回答
  •  没有蜡笔的小新
    2020-12-01 09:21

    I think your original problem was that malloc failed to allocate the requested memory on your system.

    Why this happened is specific to your system.

    When a process is loaded, it is allocated memory up to a certain address which is the system break point for the process. Beyond that address the memory is unmapped for the process. So when the process "hits" the "break" point it requests more memory from the system and one way to do this is via the system call sbrk
    malloc would do that under the hood but in your system for some reason it failed.

    There could be many reasons for this for example:
    1) I think in Linux there is a limit for max memory size. I think it is ulimit and perhaps you hit that. Check if it is set to a limit
    2) Perhaps your system was too loaded
    3) Your program does bad memory management and you end up with fragemented memory so malloc can not get the chunk size you requested.
    4) Your program corrupts the malloc internal data structures i.e. bad pointer usage
    etc

提交回复
热议问题