google/goexpect

Possible memory leak because of Ticker.

marek5050 opened this issue · 2 comments

chTicker := time.NewTicker(check)

Hello,
After running expect a couple of thousand times it seems there's a memory leak that occurs. I think I've traced it down to the referenced lines and reading through the NewTicker comments they recommend:

// The duration d must be greater than zero; if not, NewTicker will panic.
// Stop the ticker to release associated resources.

I couldn't find the code to stop the Ticker within expect.go. Is there a reason there's no Stop()?

defer chTicker.Stop()

Heap pprof profile without Stop

68.93MB of 68.93MB total (  100%)
Dropped 52 nodes (cum <= 0.34MB)
      flat  flat%   sum%        cum   cum%
      46MB 66.74% 66.74%       46MB 66.74%  runtime.makechan
   11.50MB 16.68% 83.42%    45.85MB 66.51%  time.NewTicker
       4MB  5.80% 89.22%    18.50MB 26.84%  time.NewTimer
    2.85MB  4.13% 93.35%     2.85MB  4.13%  runtime.addtimerLocked
    0.88MB  1.28% 94.63%     0.88MB  1.28%  compress/flate.NewWriter
    0.64MB  0.92% 95.55%     0.64MB  0.92%  github.com/golang/glog.(*syncBuffer).rotateFile

Heap pprof profile with Stop

18236.91kB of 18236.91kB total (  100%)
Dropped 64 nodes (cum <= 91.18kB)
      flat  flat%   sum%        cum   cum%
 9728.73kB 53.35% 53.35%  9728.73kB 53.35%  runtime.makechan
 5632.34kB 30.88% 84.23% 15361.08kB 84.23%  time.NewTimer
 1121.44kB  6.15% 90.38%  1121.44kB  6.15%  runtime.addtimerLocked
  650.62kB  3.57% 93.95%   650.62kB  3.57%  github.com/golang/glog.(*syncBuffer).rotateFile
  591.75kB  3.24% 97.19%   591.75kB  3.24%  crypto/elliptic.initTable
  512.02kB  2.81%   100%   512.02kB  2.81%  vendor/golang_org/x/net/http2/hpack.addDecoderNode

Example code:
https://github.com/marek5050/GoPprof/blob/master/src/github.com/marek5050/GoProfiling/cmd/lser/main.go

Hey marek5050.

Sorry for not noticing this until now .. Will have a look in a bit.

Hey marek5050..

Thx. for finding/fixing that one! ..