Looks like keeping a limit on the number of ScriptScope's being used is
the best approach for a long running process which makes sense. The code
below does not have a memory usage problem.
class Program
{
private static Queue<ScriptScope> m_scriptScopes = new
Queue<ScriptScope>();
private static AutoResetEvent m_scriptScopeAvailable = new
AutoResetEvent(false);
static void Main(string[] args)
{
ScriptRuntime m_scriptRuntime = IronRuby.CreateRuntime();
m_scriptScopes.Enqueue(m_scriptRuntime.CreateScope("IronRuby"));
m_scriptScopes.Enqueue(m_scriptRuntime.CreateScope("IronRuby"));
m_scriptScopes.Enqueue(m_scriptRuntime.CreateScope("IronRuby"));
for (int index = 0; index < 10000; index++)
{
ScriptScope rubyScope = null;
while (rubyScope == null)
{
lock (m_scriptScopes)
{
if (m_scriptScopes.Count > 0)
{
rubyScope = m_scriptScopes.Dequeue();
}
else
{
m_scriptScopeAvailable.Reset();
}
}
if (rubyScope == null)
{
m_scriptScopeAvailable.WaitOne();
}
}
ThreadPool.QueueUserWorkItem(new WaitCallback(RunRubyScript), new
object[] {rubyScope, index});
}
Console.ReadLine();
}
private static void RunRubyScript(object state)
{
ScriptScope rubyScope = (ScriptScope)((object[])state)[0];
int count = (int)((object[])state)[1];
rubyScope.ClearVariables();
rubyScope.SetVariable("local1", count);
rubyScope.SetVariable("local2", count);
rubyScope.Execute("print \"#{local1} - #{local2}\n\"");
lock (m_scriptScopes)
{
m_scriptScopes.Enqueue(rubyScope);
m_scriptScopeAvailable.Set();
}
}
}
Regards,
Aaron
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ironruby-core mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core