From 6b50060eae286192609c9d0a4accd128c2a7ac1f Mon Sep 17 00:00:00 2001 From: zhouyuyan Date: Tue, 12 Sep 2017 09:17:36 +0800 Subject: [PATCH] free memory --- broker/broker.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/broker/broker.go b/broker/broker.go index d85b4291..82f00f3a 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -11,6 +11,7 @@ import ( "time" "github.com/eclipse/paho.mqtt.golang/packets" + "github.com/shirou/gopsutil/mem" "golang.org/x/net/websocket" @@ -84,12 +85,14 @@ func (b *Broker) Start() { } func StateMonitor() { - // v, _ := mem.VirtualMemory() - timeSticker := time.NewTicker(time.Second * 30) + v, _ := mem.VirtualMemory() + timeSticker := time.NewTicker(time.Second * 5) for { select { case <-timeSticker.C: - debug.FreeOSMemory() + if v.UsedPercent > 0.8 { + debug.FreeOSMemory() + } // fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent) } }