测试内存泄露问题

This commit is contained in:
junleea 2025-03-10 15:00:42 +08:00
parent 8c382bbbff
commit 539ea9215b
2 changed files with 4 additions and 9 deletions

View File

@ -195,13 +195,11 @@ func subscribeAndHandleMessagesV5(ws *websocket.Conn, deviceId int) {
count := 0 count := 0
//定时器,发送计数器 //定时器,发送计数器
tCount := 0 tCount := 0
var buf []byte buf := make([]byte, 1024)
//计算帧率 //计算帧率
for { for {
//将buf置空
buf = nil
//从service获取当前帧 //从service获取当前帧
c := service.GetDeviceCurrentFrameV5(deviceId, count, &buf) c := service.GetDeviceCurrentFrameV5(deviceId, count, buf)
if c != count { if c != count {
count = c count = c
err2 := ws.WriteMessage(websocket.BinaryMessage, buf) err2 := ws.WriteMessage(websocket.BinaryMessage, buf)
@ -233,9 +231,6 @@ func subscribeAndHandleMessagesV5(ws *websocket.Conn, deviceId int) {
worker.SetRedisWithExpire(strconv.Itoa(deviceId)+"_is_play", "1", time.Minute*5) worker.SetRedisWithExpire(strconv.Itoa(deviceId)+"_is_play", "1", time.Minute*5)
log.Printf("device_id: %d has set is_play to 0", deviceId) log.Printf("device_id: %d has set is_play to 0", deviceId)
} }
//垃圾回收
runtime.GC()
} }
func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) { func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) {

View File

@ -92,7 +92,7 @@ func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error {
DeviceFrameCount[deviceId] = frameCount DeviceFrameCount[deviceId] = frameCount
return nil return nil
} }
func GetDeviceCurrentFrameV5(deviceId int, curCount int, buf *[]byte) (cnt int) { func GetDeviceCurrentFrameV5(deviceId int, curCount int, buf []byte) (cnt int) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Printf("设备:%d 错误: %v\n", deviceId, err) log.Printf("设备:%d 错误: %v\n", deviceId, err)
@ -143,7 +143,7 @@ func GetDeviceCurrentFrameV5(deviceId int, curCount int, buf *[]byte) (cnt int)
} }
// 避免直接返回大数组,可考虑复制必要的数据 // 避免直接返回大数组,可考虑复制必要的数据
if len(tempBuf) > 0 { if len(tempBuf) > 0 {
copy(*buf, tempBuf) copy(buf, tempBuf)
// 释放临时变量 // 释放临时变量
tempBuf = nil tempBuf = nil
} }