Skip to content

Commit d9f6f5e

Browse files
author
nce40202
committed
feat(内存可视化): 实现动态年龄判定功能并添加TargetSurvivorRatio参数
动态年龄判定功能已实现,允许根据Survivor区目标使用率决定对象是否晋升到老年代。同时,添加了-XX:TargetSurvivorRatio参数,用于配置Survivor区的目标使用率,默认值为50%
1 parent 6add26c commit d9f6f5e

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/components/jvm-memory-visualizer/LogicTable.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ const logicList = ref<LogicItem[]>([
5656
{
5757
id: 'dynamic_age_threshold',
5858
name: '动态年龄判定',
59-
desc: '当 Survivor 区中相同年龄对象大小总和超过 Survivor 区的一半时,年龄大于等于该年龄的对象直接进入老年代',
60-
enabled: false,
59+
desc: '当 Survivor 区中相同年龄对象大小总和超过 Survivor 区目标使用率时,年龄大于等于该年龄的对象直接进入老年代(可以通过 -XX:TargetSurvivorRatio 参数配置)',
60+
enabled: true,
6161
configurable: false,
62-
implemented: false
62+
implemented: true
6363
}
6464
]);
6565

src/views/MemoryVisualizer.vue

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ const jvmArgsHelp = [
180180
"desc": "对象晋升老年代的年龄阈值",
181181
"defaultValue": "15",
182182
"example": "-XX:MaxTenuringThreshold=15"
183+
},
184+
{
185+
"name": "-XX:TargetSurvivorRatio",
186+
"desc": "Survivor区目标使用率",
187+
"defaultValue": "50",
188+
"example": "-XX:TargetSurvivorRatio=50"
183189
}
184190
];
185191
@@ -230,6 +236,7 @@ const memoryConfig = ref({
230236
newRatio: 2, // 新生代和老年代的比例,默认为2,表示新生代:老年代=1:2
231237
survivorRatio: 8, // Eden区和Survivor区的比例,默认为8,表示Eden:Survivor=8:1
232238
maxTenuringThreshold: 15, // 对象晋升年龄阈值,默认为15
239+
targetSurvivorRatio: 50, // Survivor区目标使用率,默认为50%
233240
});
234241
235242
// JVM参数解析函数
@@ -239,7 +246,8 @@ const parseJvmArgs = (args: string) => {
239246
maxHeap: 1,
240247
newRatio: 2,
241248
survivorRatio: 8,
242-
maxTenuringThreshold: 15
249+
maxTenuringThreshold: 15,
250+
targetSurvivorRatio: 50
243251
};
244252
245253
const xmsMatch = args.match(/-Xms(\d+)([kmg])?/i);
@@ -299,6 +307,15 @@ const parseJvmArgs = (args: string) => {
299307
}
300308
}
301309
310+
// 解析TargetSurvivorRatio参数
311+
const targetSurvivorRatioMatch = args.match(/-XX:TargetSurvivorRatio=(\d+)/i);
312+
if (targetSurvivorRatioMatch) {
313+
result.targetSurvivorRatio = parseInt(targetSurvivorRatioMatch[1]);
314+
if (result.targetSurvivorRatio < 0 || result.targetSurvivorRatio > 100) {
315+
throw new Error('TargetSurvivorRatio必须在0到100之间');
316+
}
317+
}
318+
302319
// 验证参数
303320
if (result.initialHeap > result.maxHeap) {
304321
throw new Error('初始堆大小不能大于最大堆大小');
@@ -438,6 +455,29 @@ class gc {
438455
if (obj.age >= MAX_TENURING_THRESHOLD.value) {
439456
return put2OldGen(obj)
440457
} else {
458+
// 动态年龄判定
459+
const ageMap = new Map<number, number>();
460+
heapObjects.value
461+
.filter(o => o.space === currentFromSpace.value)
462+
.forEach(o => {
463+
ageMap.set(o.age, (ageMap.get(o.age) || 0) + o.size);
464+
});
465+
466+
let totalSize = 0;
467+
let targetAge = obj.age;
468+
for (let age = 0; age <= obj.age; age++) {
469+
totalSize += ageMap.get(age) || 0;
470+
if (totalSize > survivorSize.value * memoryConfig.value.targetSurvivorRatio / 100) {
471+
targetAge = age;
472+
break;
473+
}
474+
}
475+
476+
if (obj.age >= targetAge) {
477+
operationLogs.value.unshift(`对象 ${obj.name} 因动态年龄判定晋升到老年代`);
478+
return put2OldGen(obj);
479+
}
480+
441481
// 检查Survivor空间
442482
if (getSpaceUsed(SURVIVOR_TO.value) + obj.size <= survivorSize.value) {
443483
survivingObjects.push({

0 commit comments

Comments
 (0)