leak.go 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "runtime"
  7. "strconv"
  8. "time"
  9. "github.com/tal-tech/go-zero/core/cmdline"
  10. "github.com/tal-tech/go-zero/core/collection"
  11. "github.com/tal-tech/go-zero/core/proc"
  12. )
  13. const numItems = 1000000
  14. var round = flag.Int("r", 3, "rounds to go")
  15. func main() {
  16. defer proc.StartProfile().Stop()
  17. flag.Parse()
  18. fmt.Println(getMemUsage())
  19. for i := 0; i < *round; i++ {
  20. do()
  21. }
  22. cmdline.EnterToContinue()
  23. }
  24. func do() {
  25. tw, err := collection.NewTimingWheel(time.Second, 100, execute)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. for i := 0; i < numItems; i++ {
  30. key := strconv.Itoa(i)
  31. tw.SetTimer(key, key, time.Second*5)
  32. }
  33. fmt.Println(getMemUsage())
  34. }
  35. func execute(k, v interface{}) {
  36. }
  37. func getMemUsage() string {
  38. runtime.GC()
  39. var m runtime.MemStats
  40. runtime.ReadMemStats(&m)
  41. // For more info, see: https://golang.org/pkg/runtime/#MemStats
  42. return fmt.Sprintf("Heap Alloc = %dMiB", toMiB(m.HeapAlloc))
  43. }
  44. func toMiB(b uint64) uint64 {
  45. return b / 1024 / 1024
  46. }