Last active
December 14, 2015 20:19
-
-
Save nbkolchin/5143162 to your computer and use it in GitHub Desktop.
Demonstrate potential memory leak in fsnotify.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"os" | |
"github.com/howeyc/fsnotify" | |
"runtime" | |
) | |
func main() { | |
c := make(chan int) | |
if len(os.Args) < 2 { | |
log.Fatal("no arguments") | |
} | |
n, err := fsnotify.NewWatcher() | |
if err != nil { | |
log.Fatal(err) | |
} | |
go func() { | |
var m runtime.MemStats | |
for { | |
select { | |
case ev := <-n.Event: | |
// log.Println("event:", ev) | |
if ev.IsCreate() { | |
// log.Println("removing", ev.Name) | |
err = os.Remove(ev.Name) | |
if err != nil { | |
log.Println("remove failed:", err) | |
} | |
} | |
ev = nil | |
case err := <-n.Error: | |
log.Println("error:", err) | |
err = nil | |
} | |
runtime.GC() | |
runtime.ReadMemStats(&m) | |
log.Println(m.Alloc, m.TotalAlloc, m.Sys, m.Mallocs, m.Frees) | |
} | |
}() | |
for _, s := range os.Args[1:] { | |
err = n.WatchFlags(s, fsnotify.FSN_CREATE) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
<-c // wait forever | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment