Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when using large js library #164

Open
mopicus opened this issue Oct 30, 2019 · 1 comment
Open

Memory leak when using large js library #164

mopicus opened this issue Oct 30, 2019 · 1 comment

Comments

@mopicus
Copy link

mopicus commented Oct 30, 2019

When using typescriptServices.js file, for transpiling typescript code to javascript, it appears to be a memory leak. The size of the library is ~6MB

After each call to CallGlobalFunction, the memory goes up by 30MB, and never goes down

public sealed class ScriptCompiler
{
        private readonly ScriptEngine jSEngine;

        public ScriptCompiler()
        {
            jSEngine = new ScriptEngine();

            var compilerSourceStream = Assembly.GetAssembly(this.GetType())
                .GetManifestResourceStream("typescriptServices.js");
            var compilerSource = ReadStream(compilerSourceStream);

            jSEngine.Evaluate(new StringScriptSource(compilerSource));

            var bootstrapSourceStream = Assembly.GetAssembly(this.GetType())
                .GetManifestResourceStream("nTypescriptBootstrap.js");
            var bootstrapSource = ReadStream(bootstrapSourceStream);
            jSEngine.Evaluate(new StringScriptSource(bootstrapSource));
        }

        private string ReadStream(Stream stream)
        {
            var value = string.Empty;
            using (stream)
            {
                using (var sourceReader = new StreamReader(stream))
                {
                    value = sourceReader.ReadToEnd();
                }
            }
            return value;
        }

        public string Compile(string source)
        {
            Ensure.NotNullEmpty(source, "ScriptCompiler.Source is empty");

            try
            {
                var transpileResult = jSEngine.CallGlobalFunction("tsTranspile", source);
                var outputCode = jSEngine.CallGlobalFunction<string>("getTranspileResultCode", transpileResult);
                //Need to call GC.Collect here, otherwise memory goes up by 30MB after each call
                GC.Collect();
                return outputCode;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
}
@bbqchickenrobot
Copy link

Was this issue ever addressed or is it still occurring? Or was it ever an issue with Jurassic vs the specific script being run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants