2

I was trying to find the heap size my process is using. I did

fgrep '[stack]' /proc/pid/maps and got

00a00000-45937000 rw-p 00000000 00:00 0 [heap]

Now I wanted to calculate the size of the heap. So I did

(45937000 - 00a00000 ) = 44F37000

converted it to decimal 1156804608. Then to GB = 1156804608/(1000*1000*1000) = 1.1568 GB.

Is what I am doing correct?

vanta mula
  • 21
  • 1
  • 4

1 Answers1

1

Yes, taking the end boundary address minus the start boundary address will give you the size of the heap in that particular process.

The calculation may be carried in the shell with

hsize=$(( 0x45937000 - 0x00a00000 + 1 ))
printf 'heap is %d bytes (about %d MiB)\n' \
    "$hsize" "$(( hsize / 1024 / 1024 ))"

With GNU awk parsing the maps "file" for PID $pid (GNU awk needs -n to be able to recognize non-decimal integers):

awk -n -F '[- ]' '/\[heap\]/ {
    h = "0x" $2 - "0x" $1 + 1
    printf("heap is %d bytes (%.2f MiB)\n", h, h/1024/1024) }' /proc/$pid/maps

See also this related question: How do I read from /proc/$pid/mem under Linux?

This question on ServerFault may also be useful, depending on what you would want to do: Dump a linux process's memory to file

Kusalananda
  • 333,661