[
  {
    "path": ".gitignore",
    "content": "# Compiled Object files, Static and Dynamic libs (Shared Objects)\n*.o\n*.a\n*.so\n\n# Folders\n_obj\n_test\n\n# Architecture specific extensions/prefixes\n*.[568vq]\n[568vq].out\n\n*.cgo1.go\n*.cgo2.c\n_cgo_defun.c\n_cgo_gotypes.go\n_cgo_export.*\n\n_testmain.go\n\n*.exe\n*.test\n*.prof\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2014 Markus Zimmermann\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n"
  },
  {
    "path": "README.md",
    "content": "# go-leak [![GoDoc](https://godoc.org/github.com/zimmski/go-leak?status.png)](https://godoc.org/github.com/zimmski/go-leak)\n\nThe std packages of Go do currently not included detection for leaks. go-leak is a package which should help you find leaks in your code. If you have any ideas on how to improve this package or have problems of any kind with it, please [submit an issue](https://github.com/zimmski/go-leak/issues/new) through the [issue tracker](https://github.com/zimmski/go-leak/issues).\n\n> **Note:** This package does not work anymore. It worked for some use-cases I had with an older Go version but with >= 1.4 it does not work at all. I have it on my TODO to redo the whole thing and add some more use-cases but there are some other open source projects I have to do first. So if you have some time on your hand and would like to contribute mail me or submit an issue in the project's tracker.\n\n> **Note:** Since Go is scoped it is important to avoid new variables that are in the same scope as the mark-and-release calls.\n\n## goroutines\n\nIf you want to know if a function is leaking goroutines:\n\n```go\nleaks := leak.GoRoutineLeaks(foo)\n\nif leaks > 0 {\n\tpanic(\"foo is leaking!\")\n}\n```\n\nIf you want to know if a code is leaking goroutines:\n\n```go\nm := MarkGoRoutines()\n\n// some code\n\nleaks := m.Release()\n\nif leaks > 0 {\n\tpanic(\"some code is leaking!\")\n}\n```\n\n\n## memory\n\nIf you want to know if a function is leaking memory:\n\n```go\nleaks := leak.MemoryLeaks(foo)\n\nif leaks > 0 {\n\tpanic(\"foo is leaking!\")\n}\n```\n\nIf you want to know if a code is leaking memory:\n\n```go\nm := MarkMemory()\n\n// some code\n\nleaks := m.Release()\n\nif leaks > 0 {\n\tpanic(\"some code is leaking!\")\n}\n```\n"
  },
  {
    "path": "goroutine.go",
    "content": "package leak\n\nimport (\n\t\"runtime\"\n)\n\nfunc GoRoutineLeaks(f func()) int {\n\tthen := runtime.NumGoroutine()\n\n\tf()\n\n\tnow := runtime.NumGoroutine()\n\n\treturn now - then\n}\n\ntype GoRoutineMark struct {\n\tthen int\n\tnow  int\n}\n\nfunc MarkGoRoutines() *GoRoutineMark {\n\tm := &GoRoutineMark{}\n\n\tm.then = runtime.NumGoroutine()\n\n\treturn m\n}\n\nfunc (m *GoRoutineMark) Release() int {\n\tm.now = runtime.NumGoroutine()\n\n\treturn m.now - m.then\n}\n"
  },
  {
    "path": "goroutine_test.go",
    "content": "package leak\n\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc TestGoRoutineLeaks(t *testing.T) {\n\tleaks := GoRoutineLeaks(func() {\n\t\tc := make(chan bool)\n\n\t\tleaks := GoRoutineLeaks(func() {\n\t\t\tgo func() {\n\t\t\t\t<-c\n\t\t\t}()\n\t\t})\n\n\t\tassert.Equal(t, 1, leaks)\n\n\t\tc <- true\n\t})\n\n\tassert.Equal(t, 0, leaks)\n}\n\nfunc TestGoRoutineMark(t *testing.T) {\n\tc := make(chan bool)\n\n\tm := MarkGoRoutines()\n\n\tgo func() {\n\t\t<-c\n\t}()\n\n\tassert.Equal(t, 1, m.Release())\n\n\tc <- true\n\n\tassert.Equal(t, 0, m.Release())\n}\n"
  },
  {
    "path": "init.go",
    "content": "package leak\n\nimport (\n\t\"sync\"\n)\n\nvar fixRan = false\nvar fixMutex sync.Mutex\n\nfunc init() {\n\t// FIXME there is an allocation for the first started go routine, since I do not know where the allocation is coming from or how to fix that, I just want to ignore it by making an early goroutine. This does NOT show up during test execution. This is easily explained since AFAIK the tests are run in goroutines.\n\n\tfixMutex.Lock()\n\tdefer fixMutex.Unlock()\n\n\tif fixRan {\n\t\treturn\n\t}\n\n\tfixRan = true\n\n\tch := func() chan bool {\n\t\tch := make(chan bool)\n\n\t\tgo func() {\n\t\t\tch <- true\n\n\t\t\tclose(ch)\n\t\t}()\n\n\t\treturn ch\n\t}()\n\n\t<-ch\n}\n"
  },
  {
    "path": "memory.go",
    "content": "package leak\n\nimport (\n\t\"runtime\"\n)\n\nfunc MemoryLeaks(f func()) int {\n\tvar then = new(runtime.MemStats)\n\tvar now = new(runtime.MemStats)\n\n\truntime.GC()\n\truntime.ReadMemStats(then)\n\n\tf()\n\n\truntime.GC()\n\truntime.ReadMemStats(now)\n\n\treturn int((now.Mallocs - then.Mallocs) - (now.Frees - then.Frees))\n}\n\ntype MemoryMark struct {\n\tthen *runtime.MemStats\n\tnow  *runtime.MemStats\n}\n\nfunc MarkMemory() *MemoryMark {\n\tm := &MemoryMark{\n\t\tthen: new(runtime.MemStats),\n\t\tnow:  new(runtime.MemStats),\n\t}\n\n\truntime.GC()\n\truntime.ReadMemStats(m.then)\n\n\treturn m\n}\n\nfunc (m *MemoryMark) Release() int {\n\truntime.GC()\n\truntime.ReadMemStats(m.now)\n\n\treturn int((m.now.Mallocs - m.then.Mallocs) - (m.now.Frees - m.then.Frees))\n}\n"
  },
  {
    "path": "memory_test.go",
    "content": "package leak\n\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n)\n\ntype MemoryLeaker struct {\n\tFoo int\n}\n\nfunc TestMemoryLeaks(t *testing.T) {\n\tleaks := MemoryLeaks(func() {\n\t\tvar leaking *MemoryLeaker\n\n\t\tleaks := MemoryLeaks(func() {\n\t\t\tleaking = &MemoryLeaker{\n\t\t\t\tFoo: 123,\n\t\t\t}\n\t\t})\n\n\t\tassert.Equal(t, 1, leaks)\n\n\t\tleaking = nil\n\t})\n\n\tassert.Equal(t, 0, leaks)\n}\n\nfunc TestMemoryLeaksGoroutine(t *testing.T) {\n\tleaks := MemoryLeaks(func() {\n\t\tch := func() chan bool {\n\t\t\tch := make(chan bool)\n\n\t\t\tgo func() {\n\t\t\t\tch <- true\n\n\t\t\t\tclose(ch)\n\t\t\t}()\n\n\t\t\treturn ch\n\t\t}()\n\n\t\t<-ch\n\t})\n\n\tassert.Equal(t, 0, leaks)\n}\n\nvar leaking *MemoryLeaker\n\nfunc TestMemoryMark(t *testing.T) {\n\tm := MarkMemory()\n\n\tleaking = leakMemory()\n\n\tassert.Equal(t, 1, m.Release())\n\n\tleaking = nil\n\n\tassert.Equal(t, 0, m.Release())\n}\n\nfunc leakMemory() *MemoryLeaker {\n\treturn &MemoryLeaker{\n\t\tFoo: 123,\n\t}\n}\n"
  }
]