gc.go 380 B

123456789101112131415161718192021222324252627282930313233
  1. package utils
  2. import (
  3. "runtime"
  4. "runtime/debug"
  5. "sync"
  6. "time"
  7. )
  8. var Gmutex sync.Mutex
  9. var ReqCount int
  10. func GC() {
  11. Gmutex.Lock()
  12. defer Gmutex.Unlock()
  13. ReqCount++
  14. if ReqCount >= 100 {
  15. runtime.GC()
  16. ReqCount = 0
  17. }
  18. }
  19. func Free() {
  20. for {
  21. if time.Now().Hour() == 0 {
  22. Gmutex.Lock()
  23. debug.FreeOSMemory()
  24. Gmutex.Unlock()
  25. }
  26. time.Sleep(1 * time.Hour)
  27. }
  28. }