Skip to content

Commit 836164c

Browse files
author
nce40202
committed
refactor(MemoryVisualizer): 优化动态年龄判定逻辑并移除无用代码
重构了动态年龄判定逻辑,将 ageMap 的计算提前,避免重复计算。移除了未使用的 JVM 内存可视化容器代码,提升代码可读性和维护性。
1 parent 63ce592 commit 836164c

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/views/MemoryVisualizer.vue

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
<a-button @click="showArgsHelp" style="margin: 0 10px">参数说明</a-button>
1313
</a-form-item>
1414
<a-form-item>
15-
<a-button @click="restartJvm" class="restart-button" :loading="isRestarting">重启
16-
JVM</a-button>
15+
<a-button @click="restartJvm" class="restart-button" :loading="isRestarting">
16+
重启 JVM
17+
</a-button>
1718
</a-form-item>
1819
<a-form-item>
1920
<a-button @click="showLogicTable">内存管理逻辑</a-button>
@@ -125,15 +126,6 @@
125126
</a-space>
126127
</div>
127128

128-
<div class="jvm-memory-visualizer">
129-
130-
<div class="memory-container">
131-
132-
133-
134-
</div>
135-
</div>
136-
137129
<!-- https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html -->
138130
<a-modal v-model:visible="showHelpModal" title="JVM 参数说明" :footer="false" width="1000px">
139131
<a-table :data="jvmArgsHelp" :pagination="false">
@@ -486,6 +478,15 @@ class gc {
486478
}
487479
}
488480
481+
const ageMap = new Map<number, number>();
482+
heapObjects.value
483+
.filter(o => o.space === currentFromSpace.value)
484+
.forEach(o => {
485+
ageMap.set(o.age, (ageMap.get(o.age) || 0) + o.size);
486+
});
487+
488+
console.log(ageMap)
489+
489490
heapObjects.value
490491
.filter(obj => obj.space === currentFromSpace.value)
491492
.forEach(obj => {
@@ -500,23 +501,19 @@ class gc {
500501
return put2OldGen(obj)
501502
} else {
502503
// 动态年龄判定
503-
const ageMap = new Map<number, number>();
504-
heapObjects.value
505-
.filter(o => o.space === currentFromSpace.value)
506-
.forEach(o => {
507-
ageMap.set(o.age, (ageMap.get(o.age) || 0) + o.size);
508-
});
509-
510504
let totalSize = 0;
511-
let targetAge = obj.age;
505+
let targetAge = MAX_TENURING_THRESHOLD.value + 1;
512506
for (let age = 0; age <= obj.age; age++) {
513507
totalSize += ageMap.get(age) || 0;
508+
console.log('age:', age, 'totalSize:', totalSize, 'ration:', survivorSize.value * memoryConfig.value.targetSurvivorRatio / 100)
514509
if (totalSize > survivorSize.value * memoryConfig.value.targetSurvivorRatio / 100) {
515510
targetAge = age;
516511
break;
517512
}
518513
}
519514
515+
console.log(obj.age, 'targetAge:', targetAge)
516+
520517
if (obj.age >= targetAge) {
521518
operationLogs.value.unshift(`对象 ${obj.name} 因动态年龄判定晋升到老年代`);
522519
return put2OldGen(obj);

0 commit comments

Comments
 (0)