2

I am working on a script that creates several fairly complex nested hash datastructures and then iterates through them conditionally creating database records. This is a standalone script using active record. After several minutes of running I noticed a significant lag in server responsiveness and discovered that the script, while being set to be nice +19, was enjoying a steady %85 - %90 total server memory.

In this case I am using instance variables simply for readability. It helps knowing what is going to be re-used outside of the loop vs. what won't. Is there a reason to not use instance variables when they are not needed? Are there differences in memory allocation and management between local and instance variables? Would it help setting @variable = nil when its no longer needed?

2
  • What's the total server memory? Ruby only begins garbage collection at a certain level. Commented Jan 13, 2011 at 22:43
  • Its a 4G virtual server. Do you know at what point does it start garbage collection? Commented Jan 13, 2011 at 22:46

2 Answers 2

5

An instance variable continues to exist for the life of the object that holds it. A local variable exists only within a single method, block or module body.

If you're assuming objects in your object's instance variables will be garbage-collected just because you don't intend to refer to them in the future, that isn't how it works. The garbage collector only knows whether there's a reachable reference to the object — and there is if it's in an instance variable.

Sign up to request clarification or add additional context in comments.

4 Comments

Of course, instance v local variables probably has nothing to do with the OP's performance issue, that is just his (erroneous) guess.
@Ed Swangren: True, I wouldn't be surprised if it turned out to be something else. But it also seems like it could be related. His question is a little vague, but running a lot of computations and storing every intermediate value and result in an instance variable and having that go on for several minutes does sound like something that could eat up a lot of memory.
I also have same doubt. @Chuck can you tell me how and when the memory allocation and release happens for local and instance variables in ruby?
@Abhi: I don't 100% understand your question. Are you talking about the variables themselves (i.e. the actual pointer slots), the objects stored in the variables, or something else? AFAIK, local variables are stored on the stack, while instance variables are stored in a table associated with the object.
2

Setting @variable = nil destroys the reference to the object that the instance variable once pointed to. When there are no more remaining references to an object, it should be collected by the garbage collector eventually. I say "eventually" because the GC is somewhat unpredictable. However, it's easy to have a memory leak by a "dangling reference" and possibly (depending on how the GC is implemented) circular references. What else refers to this object?

1 Comment

a) Or setting @variable to any other value also releases the reference; b) You can call GC.start to force garbage collection to kick in if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.