0

When I run a ruby program, I assume the variables are stored in RAM. Other than when we have to page due to lack of RAM, would they ever be stored on the hard drive? I think stacks and heaps are relevant, and I couldn't find any definitive information on where ruby stores variables.

trincot
  • 317,000
  • 35
  • 244
  • 286
Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196
  • 1
    Is there something leading you to suspect this _isn't_ the case? – Matt Ball Nov 13 '13 at 03:58
  • 1
    Edited the question. You know that they are stored on RAM. Your question is whether they are ever stored on hard drive. – sawa Nov 13 '13 at 04:03
  • @MattBall Not really, but in the process of working on an app where performance matters I realized I had never considered nor researched how ruby handled variables. It's a rails app, and I realized I was relying on gems for cacheing when I might need to consider the side effects myself. – Tyler DeWitt Nov 13 '13 at 04:05
  • @sawa I wasn't positive they were stored in RAM (though I assumed), so thanks for verifying that for me. And I agree with the updated question, I am curious when/if the variables get written to the disk – Tyler DeWitt Nov 13 '13 at 04:07
  • possible duplicate of [In Ruby, what is stored on the stack?](http://stackoverflow.com/questions/13639166/in-ruby-what-is-stored-on-the-stack) – Musannif Zahir Nov 13 '13 at 04:49

3 Answers3

1

The Ruby Language Specification does not mandate or forbid any particular storage strategy. Any implementation is free to store values anywhere they want in any way. The Specification only says what the result of running a Ruby program should be, not how the program is being run. (Just like any other language specification.)

In SmallRuby, for example, objects may under some circumstances be stored on the disk. And the whole purpose of MagLev is to have a Ruby implementation which can deal with heaps that are orders of magnitude larger than RAM, by storing them in a distributed cluster on disk.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
0

The short answer is hopefully not. But the long answer is, yes, at some point if the machine runs out of memory, Ruby's variables will have to go to disk.

However, to answer your exact question: No, Ruby itself never puts variables in the hard drive (as far as I know), but at some point the operating system will deallocate Ruby's memory and send it to disk.

More interestingly, you can store ruby variables on disk in YAML files (Yet Another Markup Language), .yml. So if you're trying to store a complex object in a database with Ruby, you could use a text column, and then synthesize it to instantiate as a new object of a given class.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
OneChillDude
  • 7,856
  • 10
  • 40
  • 79
0

This answer refers to the standard MRI/YARV Ruby implementation.

Like any other process, a running Ruby program's memory is controlled by the operating system. If some other process requires more memory, the OS will try to find it. If there is no free RAM, the OS may need to temporarily store, or page the RAM of another process, perhaps your Ruby program, on the hard drive. It will be restored to RAM when it's your program's turn to run again.

It's important to note that this is completely controlled by the OS - Ruby would never actually know if its memory had been paged or not.

joews
  • 29,767
  • 10
  • 79
  • 91