Memory leaks with instance variables

Hello,

I’ve got a problem with the way ruby handles symbols (e.g. names of instance
variables).
The following ruby program creates an object and an instance variable inside
each object.
Whereas the object will be deleted by the gc when it isn’t needed any more,
the name of
the instance variable will remain in the symbol table of ruby, thus creating
memory leaks.

class TestClass
end

1000000.times{ |i|
command = "@varName#{i} = nil"
a = TestClass.new
puts "Evaluating #{command}"
a.instance_eval(command)
GC.start # try to remove old objects
puts “a.instance_variables = #{a.instance_variables}”
}

Does anyone know how to solve this problem?

Before you ask, why anybody does crazy things like creating instance
variables with instance_eval:
I use ruby as an embedded interpreter inside a test application. The state
of the test is reflected inside
ruby as an object tree. The state of a single object is represented by
instance variables created by rb_iv_set. So the test state can be checked and
altered by ruby scripts.
Stupidly ruby graps all my memory to store symbols and crashes the test.

By the way: I’m interested in gaining experiences about ruby as an embedded
interpreter. So please
drop me a line, if you have made experiences with topics like
multithreading, gc, ruby threads, the way
ruby (mis)uses the stack, …

Thanks in advance

Dirk

···


+++ GMX - Mail, Messaging & more http://www.gmx.net +++
Bitte lächeln! Fotogalerie online mit GMX ohne eigene Homepage!

Before you ask, why anybody does crazy things like creating instance
variables with instance_eval:

Well, this is not crazy to use instance_eval : your problem is not with
instance_eval but because apparently you store the state in the variable
name (@varName#{i}) and create many different symbols. Just store the
state in an instance variable and always use @varName for your variable
names.

Guy Decoux

Hi,

···

At Wed, 12 Mar 2003 23:14:44 +0900, ujb1@gmx.de wrote:

I’ve got a problem with the way ruby handles symbols (e.g. names of instance variables).
The following ruby program creates an object and an instance variable inside each object.
Whereas the object will be deleted by the gc when it isn’t needed any more, the name of
the instance variable will remain in the symbol table of ruby, thus creating
memory leaks.

Symbols can be used again, it’s not “leak”.


Nobu Nakada

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200303121439.h2CEdPh13481@moulon.inra.fr

Before you ask, why anybody does crazy things like creating
instance
variables with instance_eval:

Well, this is not crazy to use instance_eval : your problem is not
with
instance_eval but because apparently you store the state in the
variable
name (@varName#{i}) and create many different symbols. Just store
the
state in an instance variable and always use @varName for your
variable
names.

I was writing the same. Additionally the use of a fixed name could
make the use of eval superfluous, which in turn should be good for
performance.

Regards

robert