From: Gennady Bystritsky Date: 2009-07-28T03:56:17+09:00 Subject: Re: [C/Linux] How to get amount of allocated memory from within a process (heap size) > -----Original Message----- > From: kubo.takehiro@gmail.com [mailto:kubo.takehiro@gmail.com] On > Behalf Of KUBO Takehiro > Sent: Sunday, July 26, 2009 5:25 AM > To: ruby-talk ML > Subject: Re: [C/Linux] How to get amount of allocated memory from > within a process (heap size) > > On Fri, Jul 24, 2009 at 5:28 PM, Gennady > Bystritsky 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. 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 Berger's great sys-proctable gem. Once again, thank you guys very much. Ruby-talk to the rescue as usual :-) Sincerely, Gennady Bystritsky.