Function to Display Ruby Memory Usage

  • Thread starter Michael W. Ryder
  • Start date
M

Michael W. Ryder

In Business Basic there is a function, DSZ, that shows the current
amount of memory used by the program code and data. This was obviously
more useful in the past where each user might have only 16 KB of memory
available. I was wondering if Ruby had something similar while thinking
about a program. The brute force method could easily take many GB of
memory to solve the problem, while a better algorithm might take a few KB.
Something like this may be useful if developing a program to run on both
a modern PC and something like a cell phone. One could see if they are
using too much memory or if it increases as the problem gets bigger.
 
M

M. Edward (Ed) Borasky

Michael said:
In Business Basic there is a function, DSZ, that shows the current
amount of memory used by the program code and data. This was obviously
more useful in the past where each user might have only 16 KB of memory
available. I was wondering if Ruby had something similar while thinking
about a program. The brute force method could easily take many GB of
memory to solve the problem, while a better algorithm might take a few KB.
Something like this may be useful if developing a program to run on both
a modern PC and something like a cell phone. One could see if they are
using too much memory or if it increases as the problem gets bigger.

I don't know how to do this on Windows, MacOX or BSD, but on Linux, you
can pick up your PID using a call to Process.pid. Then you can shell out
to "pmap" and parse the result, which tells you all the segments and
whether they are mapped to shared libraries or inside the Ruby
executable. The code would look something like this:

pid = Process.pid
map = `pmap -d #{pid}`

# code to parse and analyze the map goes here

See

http://www.linuxdevcenter.com/pub/a/linux/2006/11/30/linux-out-of-memory.html
http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html

While you're at it, you could do the same for the PIDs of your web
server and database in Rails. :)
 
J

Jano Svitok

I don't know how to do this on Windows, MacOX or BSD, but on Linux, you
can pick up your PID using a call to Process.pid. Then you can shell out
to "pmap" and parse the result, which tells you all the segments and
whether they are mapped to shared libraries or inside the Ruby
executable. The code would look something like this:

pid = Process.pid
map = `pmap -d #{pid}`

You'll find (much simpler) version for windows in the archive in
thread 'The real difference between Mutex and Sync'. Search for
'pslist'.
 

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,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top