You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用内存泄漏排查:InMemoryAuditEventRepository问题求助

解决Spring Boot Actuator InMemoryAuditEventRepository内存泄漏问题

我之前处理过一模一样的问题,InMemoryAuditEventRepository本身就是纯内存存储审计事件的实现,默认情况下它会无限制地累积所有生成的审计事件——时间一长,事件数组越堆越大,自然就会吃掉大量内存,甚至引发内存泄漏。结合你的MAT分析结果,给你几个可行的解决思路:

1. 给内存存储设置事件数量上限(最简单的方案)

Spring Boot已经提供了配置项,可以直接限制InMemoryAuditEventRepository存储的最大事件数,超过上限后旧事件会被自动淘汰。

配置文件方式

在application.properties或者application.yml里添加:

# 保留最近1000条审计事件,可根据你的业务调整数值
management.audit.events.max-entries=1000

Java配置类方式

如果需要更灵活的控制,可以手动创建Bean并设置上限:

import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository;
import org.springframework.boot.actuate.audit.AuditEventRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AuditConfig {
    @Bean
    public AuditEventRepository auditEventRepository() {
        InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
        repository.setMaxEntries(1000); // 自定义最大存储数
        return repository;
    }
}

2. 替换为持久化的审计事件存储(适合需要保留历史数据的场景)

如果你的业务需要长期保留审计事件,不想因为内存限制丢失数据,可以换成基于数据库的实现JdbcAuditEventRepository,把事件存储到数据库中,彻底解决内存堆积问题。

步骤:

  • 确保你的项目引入了JDBC或JPA依赖(比如spring-boot-starter-jdbc或spring-boot-starter-data-jpa)
  • 创建审计事件表(可以手动建表,或者用Flyway/Liquibase做自动迁移),表结构示例:
CREATE TABLE audit_event (
    event_id BIGINT AUTO_INCREMENT PRIMARY KEY,
    event_date TIMESTAMP NOT NULL,
    principal VARCHAR(255),
    type VARCHAR(255),
    data TEXT
);
  • 完成上述配置后,Spring Boot会自动识别数据源,并将JdbcAuditEventRepository作为默认的AuditEventRepository,替代原来的内存版。

3. 自定义定时清理逻辑(适合特殊清理规则的场景)

如果你的清理需求不是按数量,而是按时间(比如保留最近24小时的事件),可以写一个定时任务定期清理内存中的旧事件:

import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.time.Instant;

@Component
@EnableScheduling // 开启定时任务支持,也可以把这个注解加在Spring Boot主类上
public class AuditEventCleaner {
    private final InMemoryAuditEventRepository auditRepo;

    // 构造注入AuditEventRepository
    public AuditEventCleaner(InMemoryAuditEventRepository auditRepo) {
        this.auditRepo = auditRepo;
    }

    @Scheduled(fixedRate = 3600000) // 每小时执行一次,可调整频率
    public void cleanupOldEvents() {
        // 清理24小时之前的所有审计事件
        Instant cutoff = Instant.now().minus(Duration.ofHours(24));
        auditRepo.getEvents().removeIf(event -> event.getTimestamp().isBefore(cutoff));
    }
}

总结

  • 快速解决内存问题选方案1,配置简单见效快;
  • 需要长期存储审计数据选方案2,彻底摆脱内存限制;
  • 有特殊清理规则选方案3,灵活定制清理逻辑。

内容的提问来源于stack exchange,提问作者IKane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:18:37