Skip to content

Memory leak using EmbedHostObject #65

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

Closed
ralfkahlert opened this issue Dec 29, 2018 · 8 comments
Closed

Memory leak using EmbedHostObject #65

ralfkahlert opened this issue Dec 29, 2018 · 8 comments

Comments

@ralfkahlert
Copy link

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
Copy link
Owner

Hello!

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

@ralfkahlert
Copy link
Author

ralfkahlert commented Dec 29, 2018 via email

@Taritsyn
Copy link
Owner

Thanks!

@Taritsyn
Copy link
Owner

Hello, Ralf!

Try to upgrade to version 3.0.1.

@ralfkahlert
Copy link
Author

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

Thanks
Ralf

@Taritsyn
Copy link
Owner

Hello, Ralf!

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

@ralfkahlert
Copy link
Author

ralfkahlert commented Jan 25, 2019 via email

@Taritsyn
Copy link
Owner

OK, then I close this issue.

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