go-ping / ping

ICMP Ping library for Go
MIT License
1.33k stars 345 forks source link

memory leak while running for some time #118

Closed sivarajendranaidu closed 3 years ago

sivarajendranaidu commented 3 years ago

I tried basic Pinger as follows:

for {
    pinger, err := ping.NewPinger("8.8.8.8")

    pinger.SetPrivileged(true)
    if err != nil {
        panic(err)
    }
    pinger.Count = 3
    pinger.Interval = time.Second
    pinger.Timeout = 3 * time.Second
    pinger.SetPrivileged(true)
    pinger.Run() // blocks until finished
    stats := pinger.Statistics()
    log.Println("%v", len(stats.Rtts) == 0)
    log.Println("%v", (stats))
}

Where start ping at t = t0 and pinger run shall be blocked for 3 secs and returns the status.

I start new pinger again for getting continuous ping statistics.

I tried finally differently by registering the callback and start the New Pinger on receiving the callback as follows:

func main() {
    pinger, err := ping.NewPinger("8.8.8.8")
    if err != nil {
        fmt.Println("Pinger Create failed: %s", err.Error())
        return err
    }

    pinger.Count = 3
    pinger.Interval = time.Second
    pinger.Timeout = 3 * time.Second
    pinger.SetPrivileged(true)
    pinger.OnFinish = onFinishCb

    err = pinger.Run()
    if err != nil {
        fmt.Println("Pinger Run failed: %s", err.Error())
        return err
    }

    wg := &sync.WaitGroup{}
    wg.Add(1)
    go shutdownHandler(wg)

    wg.Wait()
}

// onFinishCb is called when ping is finished.
func onFinishCb(stats *ping.Statistics) {
    t.latency = uint64(stats.AvgRtt / time.Millisecond)
    pinger, err := ping.NewPinger(t.peerIP)
    if err != nil {
        fmt.Println("Pinger Create failed: %s", err.Error())
        // return err
    }
    pinger.Count = 3
    pinger.Interval = time.Second
    pinger.Timeout = 3 * time.Second
    pinger.SetPrivileged(true)
    pinger.OnFinish = onFinishCb
    fmt.Println("Latency :%v", t.latency)

    err = pinger.Run()
    if err != nil {
        fmt.Println("Pinger Run failed: %s", err.Error())
    }
}

In either case I am finding memory of this main process increases from 1 MB to 8 MB in 4-5 mins (with top command).

Please provide your inputs.

stevenaldinger commented 3 years ago

I'm seeing this as well, does anyone have a clue what could be causing that?

SuperQ commented 3 years ago

The "memory leak" is the packet rtt tracking feature.

This can be turned off with pinger.RecordRtts = false.

Fixed by: