robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

Memory is slowly increasing until OOM #491

Closed lrfdehao closed 1 year ago

lrfdehao commented 1 year ago

I wonder why my vm.Run() is causing the program's memory to grow slowly.

Showing nodes accounting for 233MB, 83.09% of 280.42MB total
Dropped 268 nodes (cum <= 1.40MB)
Showing top 10 nodes out of 200
      flat  flat%   sum%        cum   cum%
  142.73MB 50.90% 50.90%   142.73MB 50.90%  github.com/robertkrimen/otto/parser.ParseFileWithSourceMap
image

can i release the Value{} object memory by myself?

Asday commented 1 year ago

Can you provide a code example showing the issue?

lrfdehao commented 1 year ago
func a() []struct {
        var res []struct
        vm := otto.New()
        _, err = vm.Run(src) // src: js script , too long
        if err != nil {
                return nil, err
        }
    if value, err := vm.Get("prefix"); err == nil {
                for _, v := range value.Object().Keys() {
                        data, _ := data.Export()
                        if data, ok := data.([]map[string]interface{}); ok {
                                for _, y := range data {
                                        // res append
                                        // some logic append []struct
                                }
                                return &res
                        }
                }
        }
}
Asday commented 1 year ago

// src: js script , too long

The problem is likely in here - could you try cutting the javascript down to something minimal to demonstrate the issue? You could even start off with an empty script perhaps, to prove the issue isn't in the rest of the code.

lrfdehao commented 1 year ago

All right, thank you. I'll do a double check here