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 caused by calling RunScript on same context. #150

Closed
panzhongxian opened this issue Jun 29, 2021 · 2 comments
Closed

Memory leak caused by calling RunScript on same context. #150

panzhongxian opened this issue Jun 29, 2021 · 2 comments

Comments

@panzhongxian
Copy link

panzhongxian commented Jun 29, 2021

package main

import (
	"flag"

	"rogchap.com/v8go"
)

func no_mem_leak() {
	iso, _ := v8go.NewIsolate()     // creates a new JavaScript VM
	ctx1, _ := v8go.NewContext(iso) // new context within the VM
	ctx1.RunScript("const multiply = (a, b) => a * b", "math.js")

	ctx2, _ := v8go.NewContext(iso) // another context on the same VM
	for i := 0; i < 1000000000; i++ {
		if _, err := ctx2.RunScript("multiply(3, 4)", "main.js"); err != nil {
			// this will error as multiply is not defined in this context
		}
	}
}

func mem_leak() {
	iso, _ := v8go.NewIsolate()    // creates a new JavaScript VM
	ctx, _ := v8go.NewContext(iso) // new context within the VM
	ctx.RunScript("const multiply = (a, b) => a * b", "math.js")

	for i := 0; i < 1000000000; i++ {
		if _, err := ctx.RunScript("multiply(3, 4)", "main.js"); err != nil {
			// this will error as multiply is not defined in this context
		}
	}
}

func main() {
	memLeakFlag := flag.Bool("mem_leak", false, "mem_leak")
	flag.Parse()
	if *memLeakFlag {
		mem_leak()
	} else {
		no_mem_leak()
	}
}

When I called RunScript on same context, a possible memory leak problem may happen. Is this a bug? Or I used the context in a wrong way?

The above code is what I have run. And the memory:

image

@dylanahsmith
Copy link
Collaborator

This is the same memory leak as #105, although the title of that makes it sound like a more limited.

What is happening is that v8go keeps a reference to all v8go.Value objects that it creates, including the one returned from RunScript. This memory is held onto until the isolate is disposed.

@panzhongxian
Copy link
Author

Hi, @dylanahsmith . Thanks for your redirection and I saw the full discussion. Hope the implementation will be finished soon.

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