Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the physical memory free on the machine in Ruby?

Tags:

memory

ruby

I would like to know how much physical memory is available on the system, excluding any swap. Is there a method to get this information in Ruby?

like image 535
Guillaume Coté Avatar asked Oct 27 '10 01:10

Guillaume Coté


People also ask

How to check memory usage in Ruby?

The Memory Profiler gem is an effective way of tracking the memory usage of ruby code. This command will send a bunch of requests to your application, and track memory usage over time. In case of a memory leak, the memory usage will keep on increasing over time.

How does Ruby manage memory?

For Dynamic Memory allocation, the Ruby program uses Heap memory and the basic unit of the heap is a slot. Here, each slot occupies a value which is known as RVALUE. This RVALUE comprises 40 bytes and a container for objects of all types (Array, String, Class).


1 Answers

If you are using linux,You usually use a "free" command to find physical memory ie RAM details on the system

 output = %x(free)

output will look slightly like the following string

" total used free shared buffers cached\nMem: 251308 201500 49808 0 3456 48508\n-/+ buffers/cache: 149536 101772\nSwap: 524284 88612 435672\n"

You can extract the information you need using simple string manipulations like

output.split(" ")[7] will give total memory output.split(" ")[8] will give used memory output.split(" ")[9] will give free memory

like image 115
Rishav Rastogi Avatar answered Sep 23 '22 22:09

Rishav Rastogi