Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top