Memory usage optimization

This commit is contained in:
nuknal
2024-06-15 13:05:42 +08:00
parent 0c17fee9c7
commit 35bf364e97
7 changed files with 84 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"os"
"runtime"
"sync"
log "github.com/sirupsen/logrus"
@@ -46,6 +47,7 @@ func (rrc *RRC) Close() {}
// 统计探元灰度的累积概率密度
func (rrc *RRC) StatisticalPAN(dsfile string) error {
printMemStats()
f, err := os.Open(dsfile)
if err != nil {
return err
@@ -61,7 +63,7 @@ func (rrc *RRC) StatisticalPAN(dsfile string) error {
// 并发处理
var wg sync.WaitGroup
jobs := make(chan struct{}, 5)
var hists []*ProbeHistogram
var mutex sync.Mutex
for _, file := range files {
wg.Add(1)
@@ -86,20 +88,26 @@ func (rrc *RRC) StatisticalPAN(dsfile string) error {
}
var hist ProbeHistogram
hist.init(PANCameraProbeNum)
hist.init0(PANCameraProbeNum)
hist.statistical(img)
img.Close()
mutex.Lock()
hists = append(hists, &hist)
log.Println("sum PAN histogram...")
rrc.Histograms[0].sum([]*ProbeHistogram{&hist})
hist.free()
printMemStats()
runtime.GC()
printMemStats()
mutex.Unlock()
}(file)
}
wg.Wait()
log.Println("sum PAN histogram...")
rrc.Histograms[0].sum(hists)
log.Println("compute PAN histogram...")
rrc.Histograms[0].compute()
log.Println("save PAN gray table matrix.")
@@ -109,6 +117,7 @@ func (rrc *RRC) StatisticalPAN(dsfile string) error {
}
func (rrc *RRC) StatisticalMSS(dsfile string) error {
printMemStats()
f, err := os.Open(dsfile)
if err != nil {
return err
@@ -124,7 +133,7 @@ func (rrc *RRC) StatisticalMSS(dsfile string) error {
var wg sync.WaitGroup
jobs := make(chan struct{}, 5)
var hists [4][]*ProbeHistogram
var mutex sync.Mutex
for _, file := range files {
wg.Add(1)
@@ -141,7 +150,7 @@ func (rrc *RRC) StatisticalMSS(dsfile string) error {
}
width := MSSCameraProbeNum
height := len(data) / (width * 2)
height := len(data) / (width * 4 * 2)
mssData := make([][]byte, 4)
for h := 0; h < height; h++ {
row := data[h*width*4*2 : (h+1)*width*4*2]
@@ -159,12 +168,19 @@ func (rrc *RRC) StatisticalMSS(dsfile string) error {
}
var hist ProbeHistogram
hist.init(PANCameraProbeNum)
hist.init0(MSSCameraProbeNum)
hist.statistical(mssImages[i])
mssImages[i].Close()
mutex.Lock()
hists[i] = append(hists[i], &hist)
log.Println("sum MSS histogram...")
rrc.Histograms[i+1].sum([]*ProbeHistogram{&hist})
hist.free()
printMemStats()
runtime.GC()
printMemStats()
mutex.Unlock()
}
}(file)
@@ -172,8 +188,6 @@ func (rrc *RRC) StatisticalMSS(dsfile string) error {
wg.Wait()
for i := 1; i < 5; i++ {
log.Println("sum MSS histogram...")
rrc.Histograms[i].sum(hists[i-1])
log.Println("compute MSS histogram...")
rrc.Histograms[i].compute()
log.Println("save MSS gray table matrix.")