# Function to Display Ruby Memory Usage

**URL:** <https://www.ruby-forum.com/t/function-to-display-ruby-memory-usage/125205>\
**Category:** Ruby\
**Created:** [January 7, 2008, 2:35am UTC](https://www.ruby-forum.com/t/function-to-display-ruby-memory-usage/125205 "2008-01-07T02:35:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 7, 2008, 2:35am UTC](https://www.ruby-forum.com/t/function-to-display-ruby-memory-usage/125205/1 "2008-01-07T02:35:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 7, 2008, 5:07am UTC](https://www.ruby-forum.com/t/function-to-display-ruby-memory-usage/125205/2 "2008-01-07T05:07:44Z")

</div>

Michael W. Ryder wrote:

> 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

> **[Radar - O’Reilly](https://www.oreilly.com/radar/)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

[http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.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. 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 7, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/function-to-display-ruby-memory-usage/125205/3 "2008-01-07T15:58:56Z")

</div>

On Jan 7, 2008 5:06 AM, M. Edward (Ed) Borasky [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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