fix: 收敛战斗内存增长并强化战斗结束清理

This commit is contained in:
panw
2026-03-18 16:46:52 +08:00
parent 56227d8f3f
commit 035066752c
7 changed files with 155 additions and 58 deletions

View File

@@ -19,7 +19,15 @@ export class Skill extends ecs.Entity {
private debugMode: boolean = false;
/** 多键对象池Map<prefabPath, NodePool> */
static pools: Map<string, NodePool> = new Map();
static readonly MAX_POOL_SIZE: number = 128;
static readonly MAX_POOL_SIZE: number = 48;
static readonly MAX_POOL_TOTAL: number = 192;
private static totalPoolSize(): number {
let total = 0;
this.pools.forEach((pool) => {
total += pool.size();
});
return total;
}
static getFromPool(path: string): Node | null {
if (this.pools.has(path)) {
@@ -40,7 +48,7 @@ export class Skill extends ecs.Entity {
this.pools.set(path, new NodePool());
}
const pool = this.pools.get(path)!;
if (pool.size() >= this.MAX_POOL_SIZE) {
if (pool.size() >= this.MAX_POOL_SIZE || this.totalPoolSize() >= this.MAX_POOL_TOTAL) {
node.destroy();
return;
}
@@ -49,6 +57,12 @@ export class Skill extends ecs.Entity {
static clearPools() {
this.pools.forEach((pool) => {
while (pool.size() > 0) {
const node = pool.get();
if (node && node.isValid) {
node.destroy();
}
}
pool.clear();
});
this.pools.clear();
@@ -62,7 +76,8 @@ export class Skill extends ecs.Entity {
return {
paths: this.pools.size,
total,
maxPerPath: this.MAX_POOL_SIZE
maxPerPath: this.MAX_POOL_SIZE,
maxTotal: this.MAX_POOL_TOTAL
};
}
@@ -229,6 +244,7 @@ export class Skill extends ecs.Entity {
Skill.putToPool(this.prefabPath, this.skillNode);
this.skillNode = null;
}
this.prefabPath = "";
super.destroy();