go-win64api icon indicating copy to clipboard operation
go-win64api copied to clipboard

Memory leak while using firewall.go

Open pepperoni01 opened this issue 3 years ago • 5 comments

Hey @iamacarpet.

I'm trying to use a part of a package to control windows firewall.

It works well, but it doesn't release memory. For example, running functions in a loop will cause continuous incremental memory usage.

Looking at the library, it seems that this topic is taken care of(functions like firewallAPIRelease, firewallRulesEnumRealease).

Here's an example of code that replicates an issue.

package main

import (
    "fmt"
    winapi "github.com/kumako/go-win64api"
    "time"
)

func main(){
    var first string
    for 1>0 {
        fmt.Scanln(&first)
        if first == "run"{
            firewallTest()
        }
    }
}

func firewallTest(){
    //get the list of the rules
    ruleList, _ := winapi.FirewallRulesGet()
    //delete them one by one using their names
    for _, eachRule := range ruleList {
        ruleName := eachRule.Name
        if ruleName != ""{
            winapi.FirewallRuleDelete(ruleName)
        }
    }
    //wait for 30 second
    time.Sleep(30 * time.Second)
    //add them back, one by one, from the list
    for _, eachRule := range ruleList{
        winapi.FirewallRuleAddAdvanced(eachRule)
    }
}

pepperoni01 avatar Jun 28 '21 11:06 pepperoni01

@pepperoni01 this is hopefully fixed by a PR submitted by @cuongvn98

Please let us know if we can close it

iamacarpet avatar Mar 14 '22 10:03 iamacarpet

Unfortunately, memory leak still occurred while using the exact same code provided above. https://prnt.sc/nBBcBxH0LJXH (This was after just one "run" command, additional "run" commands continue to increase memory usage) https://prnt.sc/dLppMZK0iVBd (second "run")

pepperoni01 avatar Mar 14 '22 10:03 pepperoni01

Thanks for the update @pepperoni01

I’m not really going to be able to dedicate time to this myself, but perhaps @cuongvn98 might be happy to work with you?

Also a long shot, but have you tried forcing Go’s garbage collector to run between calls?

I think you can force it in the ‘runtime’ package, as I know from experience with other pieces of code that it will allow memory to rise dramatically if it is running on a high memory machine, before attempting to reap.

iamacarpet avatar Mar 14 '22 11:03 iamacarpet

Yes, I've tried forcing Go's garbage collector. It does something, but not much. For comparison to previous screens, forcing garbage collectors resulted this outcome: 1st run: https://prnt.sc/9oT-45yrjcL7 2nd run: https://prnt.sc/6XaaM4vSu2SI

pepperoni01 avatar Mar 14 '22 12:03 pepperoni01

@pepperoni01 As my investigate all action as oleutil.GetProperty, ToIDispatch should be graceful release after use. It seem as memory of this variable is out of manage by Go .

hirosumee avatar Mar 14 '22 14:03 hirosumee