# How to get amount of allocated memory from within a process (heap size)

**URL:** <https://www.ruby-forum.com/t/how-to-get-amount-of-allocated-memory-from-within-a-process-heap-size/172634>\
**Category:** Ruby\
**Created:** [July 24, 2009, 10:29am UTC](https://www.ruby-forum.com/t/how-to-get-amount-of-allocated-memory-from-within-a-process-heap-size/172634 "2009-07-24T10:29:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [July 24, 2009, 10:29am UTC](https://www.ruby-forum.com/t/how-to-get-amount-of-allocated-memory-from-within-a-process-heap-size/172634/1 "2009-07-24T10:29:10Z")

</div>

What would be a good way to determine the current heap size (VSZ) for a  
process on Linux?

I always used sbrk(0) for that, which used to return the top of the  
heap. However recently, to my dismay, I discovered that it does not work  
on Linux – I successfully malloc()-ed 1GB, could confirm it with “ps -o  
pid,vsz,args”, and yet sbrk(0) always returns the same value as right in  
the beginning.

As a work around, I have to open /proc//stat and read out the 23d  
field from there. However it is heavy and ugly, and I have a feeling  
that such a thing should be done in a more elegant way.

Please help. Any suggestions will be greatly appreciated. I even tried  
to play with mallinfo(), however could not figure out how to interpret  
results.

Thank you,  
Gennady B…

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [July 24, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/how-to-get-amount-of-allocated-memory-from-within-a-process-heap-size/172634/2 "2009-07-24T18:32:27Z")

</div>

> I always used sbrk(0) for that, which used to return the top of the  
> to play with mallinfo(), however could not figure out how to interpret  
> results.

gem install sys-proctable

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [July 24, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/how-to-get-amount-of-allocated-memory-from-within-a-process-heap-size/172634/3 "2009-07-24T18:52:21Z")

</div>

On Jul 24, 2009, at 5:50, “Daniel B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > What would be a good way to determine the current heap size (VSZ)
> 
> gem install sys-proctable
> 
> Regards,
> 
> Dan

Daniel, thanks for your suggestion. I looked how you do it on Linux,  
and see that it is exactly how I described in my workaround - via /  
proc/fd/stat. This works, however I am looking for some lighter  
solution available from a process itself. Like sbrk(0), which  
unfortunatelly is useless on Linux ☹

Thanks anyways, I will keep looking.

Gennady.

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [July 26, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/how-to-get-amount-of-allocated-memory-from-within-a-process-heap-size/172634/4 "2009-07-26T14:25:37Z")

</div>

On Fri, Jul 24, 2009 at 5:28 PM, Gennady  
Bystritsky[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What would be a good way to determine the current heap size (VSZ) for a process on Linux?
> 
> I always used sbrk(0) for that, which used to return the top of the heap. However recently, to my dismay, I discovered that it does not work on Linux – I successfully malloc()-ed 1GB, could confirm it with “ps -o pid,vsz,args”, and yet sbrk(0) always returns the same value as right in the beginning.

It this topic related to ruby?

Well, type “man malloc” and look for MMAP\_THRESHOLD.  
Increasing MMAP\_THRESHOLD large enough forces malloc() to use brk()  
instead of mmap(). sbrk(0) doesn’t care mmap()-ed memory.

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [July 27, 2009, 8:57pm UTC](https://www.ruby-forum.com/t/how-to-get-amount-of-allocated-memory-from-within-a-process-heap-size/172634/5 "2009-07-27T20:57:10Z")

</div>

> > What would be a good way to determine the current heap size (VSZ) for  
> > Well, type “man malloc” and look for MMAP\_THRESHOLD.  
> > Increasing MMAP\_THRESHOLD large enough forces malloc() to use brk()  
> > instead of mmap(). sbrk(0) doesn’t care mmap()-ed memory.

Well, it is not directly related to Ruby, I admit ;-). However, knowing  
how many bright people hang out on this list with at least one thing in  
common – love for Ruby – I decided to give it a try. And was rewarded  
for this with posts from Daniel and yourself. Thank you very much. I  
figured already that mmap() is “to blame” for sbkr(0) “failures” on  
Linux, however did not know about MMAP\_THRESHOLD.

I just wanted to get the current memory consumption by the current  
process, something externally available from “ps” as “vsz”. I ended up  
adopting as a final solution my original work-around of opening  
/proc//stat and reading out 23 field from there. Same approach is  
used in Daniel B.'s great sys-proctable gem.

Once again, thank you guys very much. Ruby-talk to the rescue as usual  
🙂

Sincerely,  
Gennady B…
