github.com/zly-app/zapp@v1.3.3/free_memory.go (about)

     1  /*
     2  -------------------------------------------------
     3     Author :       zlyuancn
     4     date:         2021/2/19
     5     Description :
     6  -------------------------------------------------
     7  */
     8  
     9  package zapp
    10  
    11  import (
    12  	"runtime/debug"
    13  	"time"
    14  )
    15  
    16  // 开始释放内存
    17  func (app *appCli) startFreeMemory() {
    18  	go app.freeMemory()
    19  }
    20  
    21  func (app *appCli) freeMemory() {
    22  	interval := app.config.Config().Frame.FreeMemoryInterval
    23  	if interval <= 0 {
    24  		return
    25  	}
    26  
    27  	t := time.NewTicker(time.Duration(interval) * time.Millisecond)
    28  	for {
    29  		select {
    30  		case <-app.baseCtx.Done():
    31  			t.Stop()
    32  			return
    33  		case <-t.C:
    34  			debug.FreeOSMemory()
    35  		}
    36  	}
    37  }