[C/Linux] How to get amount of allocated memory from within a process (heap size)

G

Gennady Bystritsky

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

I always used sbrk(0) for that, which used to return the top of the heap. H=
owever 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,arg=
s", and yet sbrk(0) always returns the same value as right in the beginning=
 
D

Daniel Berger

-----Original Message-----
From: Gennady Bystritsky [mailto:[email protected]]
Sent: Friday, July 24, 2009 2:28 AM
To: ruby-talk ML
Subject: [C/Linux] How to get amount of allocated memory from within a
process (heap size)

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/<pid>/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.

gem install sys-proctable

Regards,

Dan
 
G

Gennady Bystritsky

-----Original Message-----
From: Gennady Bystritsky [mailto:[email protected]]
Sent: Friday, July 24, 2009 2:28 AM
To: ruby-talk ML
Subject: [C/Linux] How to get amount of allocated memory from =20
within a
process (heap size)

What would be a good way to determine the current heap size (VSZ) =20
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 =20
with
"ps -o pid,vsz,args", and yet sbrk(0) always returns the same value =20
as
right in the beginning.

As a work around, I have to open /proc/<pid>/stat and read out the =20
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 =20
tried
to play with mallinfo(), however could not figure out how to =20
interpret
results.

gem install sys-proctable

Regards,

Dan

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

Thanks anyways, I will keep looking.

Gennady.=20
 
K

KUBO Takehiro

What would be a good way to determine the current heap size (VSZ) for a p= rocess 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 Linu=
x -- I successfully malloc()-ed 1GB, could confirm it with "ps -o pid,vsz,a=
rgs", and yet sbrk(0) always returns the same value as right in the beginni=
ng.

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.
 
G

Gennady Bystritsky

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]] 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)
=20
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.
=20
It this topic related to ruby?
=20
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.=20

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/<fd>/stat and rea=
ding out 23 field from there. Same approach is used in Daniel Berger's grea=
t sys-proctable gem.

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

Sincerely,
Gennady Bystritsky.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top