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
440 stars 49 forks source link

Memory leak using EmbedHostObject #65

Closed ralfkahlert closed 5 years ago

ralfkahlert commented 5 years ago

Accessing properties of registered host objects leak memory

The following examples registers a tiny host object with one property and one basic method within. Using a docker runtime with 128MB, memory will grow quickly and process will die after about 100000 calls reaching its memory limit. If the method doSomething is moved up to the class Native, everything works fine and the process stays at stable 27MB memory usage.

using System;
using JavaScriptEngineSwitcher.ChakraCore;
using JavaScriptEngineSwitcher.Core;

public class Native {
    public SomeClass SomeObject { get; set; } = new SomeClass();
}

public class SomeClass {
    public string doSomething(double i) => "SomeValue@" + i;
}

class Program
{
    static void Main(string[] args)
    {
        var newEngine = new ChakraCoreJsEngine();

        newEngine.EmbedHostObject("native", new Native());
        newEngine.Execute(@"function testFunction(i) {
                                return native.SomeObject.doSomething(i);
                            }");

        for (int i=0; i < 1000000; i++) {
            var result = newEngine.CallFunction("testFunction", new object[] { (double)i });
            if (i % 10000 == 0) Console.WriteLine($"Called test #{i}, result was {result}");
        }
    }
}

In this simple example we can work around the leak pre-evaluating the property.

        newEngine.EmbedHostObject("_native", new Native());
        newEngine.Execute(@"var native = { SomeObject: _native.SomeObject };");
Taritsyn commented 5 years ago

Hello!

Thanks for information. I will deal with this problem in 2019.

ralfkahlert commented 5 years ago

Hello Andrey!

Thanks a lot. I wish you a healthy start into the new year!

Ralf Kahlert

Taritsyn commented 5 years ago

Thanks!

Taritsyn commented 5 years ago

Hello, Ralf!

Try to upgrade to version 3.0.1.

ralfkahlert commented 5 years ago

Hi! Looks good, i'll run some integration tests tomorrow watching mem usage.

Thanks Ralf

Taritsyn commented 5 years ago

Hello, Ralf!

What results were obtained during the tests? By the way, in version 3.0.2 was made additional optimizations.

ralfkahlert commented 5 years ago

Hi,

really looks good. Stable memory utilization so far … thx!

I’ll update to 3.0.2 next week.

Ralf

Ralf Kahlert Senior Architect T +49 (231) 9742 - 973 M +49 (163) 3442270 KPS digital GmbH, Emil-Figge-Strasse 76-80, 44227 Dortmund, Germany

[www.kps.com]https://www.kps.com Pflichtangaben/Mandatory Disclosurehttps://kps.com/de/imprint.html [Travel expense management is time consuming, inconvenient,costly ... ? No longer: KPS and Qvartz implement SAP Concur]https://www.kps.com/en/news/qvartz-implements-sap-concur-expense-for-greater-transparency-and-cost-efficiency.html?utm_source=signature&utm_medium=banner&utm_campaign=jan19_qvartz_en

Von: Andrey Taritsyn notifications@github.com Antworten an: Taritsyn/JavaScriptEngineSwitcher reply@reply.github.com Datum: Freitag, 25. Januar 2019 um 09:39 An: Taritsyn/JavaScriptEngineSwitcher JavaScriptEngineSwitcher@noreply.github.com Cc: "Kahlert, Ralf" Ralf.Kahlert@kps.com, Author author@noreply.github.com Betreff: Re: [Taritsyn/JavaScriptEngineSwitcher] Memory leak using EmbedHostObject (#65)

Hello, Ralf!

What results were obtained during the tests? By the way, in version 3.0.2https://github.com/Taritsyn/JavaScriptEngineSwitcher/releases/tag/v3.0.2 was made additional optimizations.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/Taritsyn/JavaScriptEngineSwitcher/issues/65#issuecomment-457497224, or mute the threadhttps://github.com/notifications/unsubscribe-auth/Ae1DtosMvAO8P_rF6kmxo1ymq1DrgJ8iks5vGsLGgaJpZM4ZkcT7.

Taritsyn commented 5 years ago

OK, then I close this issue.