Taritsyn / JavaScriptEngineSwitcher

JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines. This library allows you to quickly and easily switch to using of another JavaScript engine.
Apache License 2.0
446 stars 49 forks source link

Out of Memory Error #98

Closed yawarkhan15 closed 2 years ago

yawarkhan15 commented 2 years ago

Hi I am using JavascriptEngineSwitcher with ChakraCore in wpf application, to exceute the javascript file and get the output from the file. Below details JavaScriptEngineSwitcher.Chakra : Version 3.7.1 JavaScriptEngineSwitcher.Core : Version 3.3

But some machines i am getting Out of memory Execptions. Error Occurred at Current Domain:Out of memory.: at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.<>c__DisplayClass33_0.<InnerGetVariableValue>b__0() at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.ScriptTaskWithResult1.Run() --- End of stack trace from previous location where exception was thrown --- at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.Invoke[T](Func1 func)## 2021-11-17 10:04:19,149 [20] ERROR Sample|ScriptTaskWithResult1|Run| Exception : Rule Engine Error:Out of memory.: at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.<>c__DisplayClass33_0.b0() at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.ScriptTaskWithResult1.Run() --- End of stack trace from previous location where exception was thrown --- at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.Invoke[T](Func1 func) at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.InnerGetVariableValue(String variableName) at JavaScriptEngineSwitcher.Core.JsEngineBase.GetVariableValue(String variableName) at Sample.UI.Utilities.ChakraCoreScriptEngine.d24.MoveNext()##`

Can u please help me with this.

Taritsyn commented 2 years ago

Hello, Yawar!

This error means that JavaScript engine ran out of memory. Much depends on JavaScript code being executed or how you use the engine.

How many instances of the ChakraCoreJsEngine class do you create? Do you use an using statements when creating instances the ChakraCoreJsEngine class or the Dispose method to destroy them?

yawarkhan15 commented 2 years ago

Hi @Taritsyn

I am using the 'using' statement and disposing the engine when process is done. I am using wpf in VSTO application so for ever new document I am trying to create a new instance of the engine and after the javascript gets executed it's getting disposed, I even tried to open a single instance it starts the execution but with in few seconds it throws out of memory exception.

I am not able to understand y it's working in some machine and not in other.

Taritsyn commented 2 years ago

Which of the exceptions is thrown: JsRuntimeException or JsEngineLoadException?

Taritsyn commented 2 years ago

I am not able to understand y it's working in some machine and not in other.

It depends on several factors: available amount of RAM, bit depth and version of operating system.

I recommend that you try creating an instance of engine with a fixed stack size:

var engine = new ChakraCoreJsEngine(new ChakraCoreSettings
{
    MaxStackSize = 1007616
});

If recommended stack size is not enough, then you can try increasing it 1.5 or 2 times.

I also recommend updating the JavaScriptEngineSwitcher.ChakraCore module to the latest version.

yawarkhan15 commented 2 years ago

I even tried to increase the MaxStackSize as you have suggested but i am still getting. I am getting JsRuntimeException.
Below is the Exception Object { "ClassName": "JavaScriptEngineSwitcher.ChakraCore.JsRt.JsEngineException", "Message": "Out of memory.", "Data": null, "InnerException": null, "HelpURL": null, "StackTraceString": " at JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorHelpers.ThrowIfError(JsErrorCode errorCode)", "RemoteStackTraceString": null, "RemoteStackIndex": 0, "ExceptionMethod": "8\nThrowIfError\nJavaScriptEngineSwitcher.ChakraCore, Version=3.7.1.0, Culture=neutral, PublicKeyToken=c608b2a8cc9e4472\nJavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorHelpers\nVoid ThrowIfError(JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorCode)", "HResult": -2146233088, "Source": "JavaScriptEngineSwitcher.ChakraCore", "WatsonBuckets": null, "ErrorCode": 131073 }

Taritsyn commented 2 years ago

"ClassName": "JavaScriptEngineSwitcher.ChakraCore.JsRt.JsEngineException"

This is definitely not an JsRuntimeException exception. It looks like you don't have enough memory even to create an instance of engine. Do you use a MemoryLimit configuration property somewhere?

Try an experiment: replace the JavaScriptEngineSwitcher.ChakraCore by the JavaScriptEngineSwitcher.V8 module.

yawarkhan15 commented 2 years ago

Hi as per your suggestion i tried to use the V8 Module but i am getting error this.progressFn is not function. While same is executing with Charka Core. Do V8 needs any additional settings.

Taritsyn commented 2 years ago

Hello, Yawar!

Is the progressFn function added to engine by using the EmbedHostObject method? Is the progressFn function implemented as an method of interface? Is the EmbedHostObject method with the progressFn item name called on instance of engine once or several times?

Taritsyn commented 2 years ago

Is the type of embedded object public and non-nested?