Increase JSRuntimeMemoryUsage for a static script
Hey,
I wrote a simple ChakraCore app where I executed a method over 100K times and I observed JSRuntimeMemoryUsage increase over period of execution.
public static void Main(string[] arguments)
{
using (var runtime = JavaScriptRuntime.Create(JavaScriptRuntimeAttributes.None, JavaScriptRuntimeVersion.VersionEdge, null))
{
JavaScriptContext context = runtime.CreateContext();
//
// Now set the execution context as being the current one on this thread.
//
using (new JavaScriptContext.Scope(context))
{
string script = "function GetNumber(){return 1;}";
string runMethod = "GetNumber();";
var max = 100000;
var i = 0;
JavaScriptValue result;
try
{
JavaScriptContext.RunScript(script, currentSourceContext++, String.Empty);
while (i++ < max)
{
result = JavaScriptContext.RunScript(runMethod, currentSourceContext++, String.Empty);
result.Release();
Console.WriteLine(i + "," + runtime.MemoryUsage);
}
}
catch (JavaScriptScriptException e)
{
PrintScriptException(e.Error);
}
catch (Exception e)
{
Console.Error.WriteLine("chakrahost: failed to run script: {0}", e.Message);
}
}
}
}
Is this expected ? Why does runtime memory usage increase even though i just call the Javascript method which is doing nothing ? I would really appreciate if anyone help me to understand the background chakracore work.
How much do you see the memory increase? I believe that each time you invoke RunScript the string that you pass in is copied into a new internal buffer and parsed to distinct bytecode, and I think we don't ever free source buffers until the context goes away because we have no way of getting it back and it's always possible that later on you might try to toString a function, etc.
Instead of invoking your function via RunScript, you could instead get a reference to it from the global object (JsGetGlobalObject to get the global object, JsCreatePropertyId to create a JsPropertyIdRef representing the GetNumber property, JsGetProperty to get the value of that property from the global) and then JsCallFunction to invoke the function. That shouldn't have the same kind of memory growth.
Ah, I see you linked to the memory growth. It looks like your memory is growing by ~256 bytes per iteration, and I would believe that comes from the storing the source buffer + bytecode + some slop and other overhead.
Thanks @MSLaguana. After calling JsCallFunction, it looks pretty stable. I did the same test and it looks the memory consumption gets stable around 10000 execution.
using (new JavaScriptContext.Scope(context))
{
string script = "function GetNumber(){return '1';}";
string runMethod = "GetNumber();";
var max = 1000000;
var i = 0;
JavaScriptValue result;
try
{
JavaScriptContext.RunScript(script, currentSourceContext++, String.Empty);
var globalObject = JavaScriptValue.GlobalObject;
var propertyId = JavaScriptPropertyId.FromString("GetNumber");
var runMethodValue = globalObject.GetProperty(propertyId);
var emprtyJsArray = new JavaScriptValue[0];
while (i++ < max)
{
JavaScriptValue outValue;
var error = Native.JsCallFunction(runMethodValue, emprtyJsArray, 1, out outValue);
if (error != JavaScriptErrorCode.NoError)
{
Console.WriteLine("Error : " + error);
}
Console.WriteLine(i + "," + runtime.MemoryUsage);
}
}
catch (JavaScriptScriptException e)
{
PrintScriptException(e.Error);
}
catch (Exception e)
{
Console.Error.WriteLine("chakrahost: failed to run script: {0}", e.Message);
}
}