From 0d18336b213ef91d5d65995152584ec8677e92fa Mon Sep 17 00:00:00 2001 From: Xiaofan <83447078+xiaofan-luan@users.noreply.github.com> Date: Mon, 7 Nov 2022 12:17:05 +0800 Subject: [PATCH] Fix GCTuner calculation overflow (#20354) Signed-off-by: xiaofan-luan Signed-off-by: xiaofan-luan --- internal/util/gc/gc_tuner.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/util/gc/gc_tuner.go b/internal/util/gc/gc_tuner.go index eaa2191327..ab7431978a 100644 --- a/internal/util/gc/gc_tuner.go +++ b/internal/util/gc/gc_tuner.go @@ -60,11 +60,16 @@ func optimizeGOGC() { totaluse := hardware.GetUsedMemoryCount() heapTarget := memoryThreshold - (totaluse - heapuse) - newGoGC := uint32(math.Floor(float64(heapTarget-heapuse) / float64(heapuse) * 100)) - if newGoGC < minGOGC { + var newGoGC uint32 + if heapTarget < heapuse { newGoGC = minGOGC - } else if newGoGC > maxGOGC { - newGoGC = maxGOGC + } else { + newGoGC = uint32(math.Floor(float64(heapTarget-heapuse) / float64(heapuse) * 100)) + if newGoGC < minGOGC { + newGoGC = minGOGC + } else if newGoGC > maxGOGC { + newGoGC = maxGOGC + } } action(newGoGC)