Hello All,
I covered this a little on my previous post about reducing the memory
footprint of JRuby, but thought it deserved a post of its own.
I am currently running multiple JRuby scripts inside the same JVM
instance, by running ScriptingContainer.runScriptlet(…)
Each script has its own ScriptingContainer instance since the scripts
need to be isolated.
Each script accesses the same set of Ruby classes. (These are the
classes that make up the vert.x API).
What I notice is that each Scripting Container ends up with its own set
of classes which represent the Ruby classes, and, as such there’s quite
a big memory overhead per script.
Ideally what I would like to do is precompile the shared Ruby classes
once and allow the different script instances to just access those
classes instead of maintaining their own instances.
Does anyone know whether that is possible with JRuby?
Cheers
Hello,
On Mon, Apr 2, 2012 at 2:38 PM, Tim F. [email protected] wrote:
Ideally what I would like to do is precompile the shared Ruby classes once
and allow the different script instances to just access those classes
instead of maintaining their own instances.
This is a bit hard to imagine what is in your mind.
I understood you want to avoid creating multiple Ruby runtime via
ScriptingContainer because of memory saving.
Do you want multiple instances of the same class definition?
For example:
irb(main):001:0> require ‘java’
=> true
irb(main):002:0> c = org.jruby.embed.ScriptingContainer.new
=> #Java::OrgJrubyEmbed::ScriptingContainer:0x482982bf
irb(main):003:0> c.run_scriptlet(“class MyClass; end”)
=> nil
irb(main):004:0> my_class_0 = c.run_scriptlet(“MyClass.new”)
=> #MyClass:0x6399f443
irb(main):005:0> my_class_1 = c.run_scriptlet(“MyClass.new”)
=> #MyClass:0x18793536
or:
irb(main):006:0> c.run_scriptlet(“@my_class_2 = MyClass.new”)
=> #MyClass:0x30fd981a
irb(main):007:0> c.run_scriptlet(“@my_class_3 = MyClass.new”)
=> #MyClass:0x1241647f
irb(main):008:0> c.get(“@my_class_2”)
=> #MyClass:0x30fd981a
irb(main):009:0> c.get(“@my_class_3”)
=> #MyClass:0x1241647f
I run above on IRB, but you can write the same code in Java.
Or something different you are thinking?
-Yoko
On 02/04/2012 20:20, Yoko H. wrote:
ScriptingContainer because of memory saving.
irb(main):004:0> my_class_0 = c.run_scriptlet(“MyClass.new”)
irb(main):008:0> c.get("@my_class_2")
=> #MyClass:0x30fd981a
irb(main):009:0> c.get("@my_class_3")
=> #MyClass:0x1241647f
I run above on IRB, but you can write the same code in Java.
Or something different you are thinking?
I don’t think this is my situation.
I have multiple scripting containers (all in the same JVM instance).
Each one accesses the same set of api classes Foo, Bar and Quux.
What I find is, for each ScriptingContainer, the JRuby runtime
generates internal classes corresponding to Foo, Bar and Quux on the
fly.
So, if I have 1000 scripts running I have a 1000 times as many generated
classes.
Since those classes are always the same, my question is: can’t JRuby
just generate all that stuff once and share it between the 1000
scripting container instances?
I was thinking along the lines of being able to pre-compile the classes
once and then let each scripting container access those pre-compiled
classes on the claspath (or whatever). I don’t know if that is possible
though.