Spring Boot 应用程序内存泄漏缓慢

sim*_*imo 4 memory-leaks cassandra spring-boot

我最近部署了一个计划每天运行的 Spring Boot 应用程序,以便解析来自 Cassandra 集群的新数据。经过大约 20 小时的工作(对 Cassandra 进行了数百万次读/写访问),出现了一些内存问题,并且在每次迭代时(异常被捕获在循环中)我都遇到了这个错误:

java.lang.RuntimeException: java.lang.IllegalStateException: java.lang.reflect.InvocationTargetException
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.createAccessorClass(ClassGeneratingPropertyAccessorFactory.java:170)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.potentiallyCreateAndRegisterPersistentPropertyAccessorClass(ClassGeneratingPropertyAccessorFactory.java:154)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.getPropertyAccessor(ClassGeneratingPropertyAccessorFactory.java:80)
    at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:425)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.getConvertingAccessor(MappingCassandraConverter.java:611)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.readEntityFromRow(MappingCassandraConverter.java:172)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.readRow(MappingCassandraConverter.java:149)
    at com.myapp.utils.CassandraIterable$1.next(CassandraIterable.java:35)
    at com.myapp.service.packer.PositionParsingService.parse(PositionParsingService.java:60)
    at com.myapp.service.packer.PositionLoadService.update(PositionLoadService.java:202)
    at com.myapp.service.packer.PositionLoadService.updateVehicle(PositionLoadService.java:129)
    at com.myapp.service.packer.PositionLoadService.updateAllVehicles(PositionLoadService.java:99)
    at com.myapp.service.packer.PositionLoadService.updateAllVehicles(PositionLoadService.java:84)
    at com.myapp.service.packer.PositionLoadService$$FastClassBySpringCGLIB$$2605bc6c.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: java.lang.reflect.InvocationTargetException
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$Evil.defineClass(ClassGeneratingPropertyAccessorFactory.java:1480)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$PropertyAccessorClassGenerator.generateCustomAccessorClass(ClassGeneratingPropertyAccessorFactory.java:282)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.createAccessorClass(ClassGeneratingPropertyAccessorFactory.java:168)
    ... 21 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$Evil.defineClass(ClassGeneratingPropertyAccessorFactory.java:1476)
    ... 23 common frames omitted
Caused by: java.lang.OutOfMemoryError: Compressed class space
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    ... 28 common frames omitted
Run Code Online (Sandbox Code Playgroud)

通过分析问题发生后产生的内存转储,我得到了这份报告:

保留 细节

从 Java 任务控制中,运行垃圾收集后,我得到:

内存使用情况

我不认为我的代码直接对泄漏负责,即使可能有一些东西没有在它应该发布的时候发布,或者可能有一些我忽略的配置问题。

我尝试将 java 从 JDK SE 8u60 升级到 8u121,并将 Spring boot 升级到 1.5.2.RELEASE 版本。这是依赖项的完整列表:

com.myapp:jar:0.0.1-SNAPSHOT
+- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.5.2.RELEASE:compile
|  +- org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile
|  |  +- (org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile - omitted for duplicate)
|  |  +- (org.springframework.boot:spring-boot-autoconfigure:jar:1.5.2.RELEASE:compile - omitted for duplicate)
|  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.2.RELEASE:compile
|  |  |  +- ch.qos.logback:logback-classic:jar:1.1.11:compile
|  |  |  |  +- ch.qos.logback:logback-core:jar:1.1.11:compile
|  |  |  |  \- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.22; omitted for duplicate)
|  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.24:compile
|  |  |  |  \- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.22; omitted for duplicate)
|  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.24:compile
|  |  |  |  \- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.22; omitted for duplicate)
|  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.24:compile
|  |  |     \- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.22; omitted for duplicate)
|  |  +- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
|  |  \- org.yaml:snakeyaml:jar:1.17:runtime
|  +- org.springframework.boot:spring-boot-starter-aop:jar:1.5.2.RELEASE:compile
|  |  +- (org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile - omitted for duplicate)
|  |  +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
|  |  |  +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  \- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  \- org.aspectj:aspectjweaver:jar:1.8.9:compile
|  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.2.RELEASE:compile
|  |  +- (org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile - omitted for duplicate)
|  |  +- org.apache.tomcat:tomcat-jdbc:jar:8.5.11:compile
|  |  |  \- org.apache.tomcat:tomcat-juli:jar:8.5.11:compile
|  |  \- org.springframework:spring-jdbc:jar:4.3.7.RELEASE:compile
|  |     +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |     +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |     \- (org.springframework:spring-tx:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  +- org.hibernate:hibernate-core:jar:5.0.12.Final:compile
|  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
|  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
|  |  +- org.javassist:javassist:jar:3.21.0-GA:compile (version managed from 3.18.1-GA)
|  |  +- antlr:antlr:jar:2.7.7:compile
|  |  +- org.jboss:jandex:jar:2.0.0.Final:compile
|  |  +- dom4j:dom4j:jar:1.6.1:compile
|  |  \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
|  |     \- (org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile - omitted for duplicate)
|  +- org.hibernate:hibernate-entitymanager:jar:5.0.12.Final:compile
|  |  +- (org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile - omitted for duplicate)
|  |  +- (org.hibernate:hibernate-core:jar:5.0.12.Final:compile - omitted for duplicate)
|  |  +- (dom4j:dom4j:jar:1.6.1:compile - omitted for duplicate)
|  |  +- (org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile - omitted for duplicate)
|  |  +- (org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile - omitted for duplicate)
|  |  \- (org.javassist:javassist:jar:3.21.0-GA:compile - version managed from 3.18.1-GA; omitted for duplicate)
|  +- javax.transaction:javax.transaction-api:jar:1.2:compile
|  +- org.springframework.data:spring-data-jpa:jar:1.11.1.RELEASE:compile
|  |  +- org.springframework.data:spring-data-commons:jar:1.13.1.RELEASE:compile
|  |  |  +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  +- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.22; omitted for duplicate)
|  |  |  \- (org.slf4j:jcl-over-slf4j:jar:1.7.24:runtime - omitted for duplicate)
|  |  +- org.springframework:spring-orm:jar:4.3.7.RELEASE:compile
|  |  |  +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  +- (org.springframework:spring-jdbc:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  \- (org.springframework:spring-tx:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
|  |  |  +- (org.springframework:spring-aop:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  |  \- (org.springframework:spring-expression:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- (org.springframework:spring-aop:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- (org.springframework:spring-tx:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
|  |  |  \- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- org.slf4j:slf4j-api:jar:1.7.24:compile
|  |  \- (org.slf4j:jcl-over-slf4j:jar:1.7.24:compile - scope updated from runtime; omitted for duplicate)
|  \- org.springframework:spring-aspects:jar:4.3.7.RELEASE:compile
|     \- (org.aspectj:aspectjweaver:jar:1.8.9:compile - omitted for duplicate)
+- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
|  +- (org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile - omitted for duplicate)
|  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.2.RELEASE:compile
|  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile
|  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.11:compile
|  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.11:compile
|  |     \- (org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile - omitted for duplicate)
|  +- org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
|  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
|  |  +- (org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile - omitted for duplicate)
|  |  \- com.fasterxml:classmate:jar:1.3.3:compile (version managed from 1.3.1)
|  +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile
|  |  +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile - omitted for duplicate)
|  |  \- (com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile - omitted for duplicate)
|  +- org.springframework:spring-web:jar:4.3.7.RELEASE:compile
|  |  +- (org.springframework:spring-aop:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  +- (org.springframework:spring-context:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  \- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  \- org.springframework:spring-webmvc:jar:4.3.7.RELEASE:compile
|     +- (org.springframework:spring-aop:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     +- (org.springframework:spring-context:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     +- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile
|     |  \- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     \- (org.springframework:spring-web:jar:4.3.7.RELEASE:compile - omitted for duplicate)
+- org.springframework.boot:spring-boot-starter-data-cassandra:jar:1.5.2.RELEASE:compile
|  +- (org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile - omitted for duplicate)
|  +- org.springframework:spring-tx:jar:4.3.7.RELEASE:compile
|  |  +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  \- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  \- org.springframework.data:spring-data-cassandra:jar:1.5.1.RELEASE:compile
|     +- org.springframework.data:spring-cql:jar:1.5.1.RELEASE:compile
|     |  +- (org.springframework:spring-context:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     |  +- (org.springframework:spring-beans:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     |  +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     |  +- (org.springframework:spring-expression:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     |  +- (org.springframework:spring-tx:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     |  +- (org.springframework.data:spring-data-commons:jar:1.13.1.RELEASE:compile - omitted for duplicate)
|     |  +- (com.datastax.cassandra:cassandra-driver-core:jar:3.1.4:compile - version managed from 3.1.3; omitted for duplicate)
|     |  +- (com.google.guava:guava:jar:19.0:compile - omitted for conflict with 16.0.1)
|     |  +- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.7; omitted for duplicate)
|     |  \- (org.slf4j:jcl-over-slf4j:jar:1.7.24:runtime - omitted for duplicate)
|     +- (org.springframework:spring-expression:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|     +- (org.springframework.data:spring-data-commons:jar:1.13.1.RELEASE:compile - omitted for duplicate)
|     +- (com.datastax.cassandra:cassandra-driver-core:jar:3.1.4:compile - version managed from 3.1.3; omitted for duplicate)
|     +- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.7; omitted for duplicate)
|     \- (org.slf4j:jcl-over-slf4j:jar:1.7.24:compile - scope updated from runtime; omitted for duplicate)
+- org.springframework.boot:spring-boot-devtools:jar:1.5.2.RELEASE:compile
|  +- org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile
|  |  +- (org.springframework:spring-core:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  |  \- (org.springframework:spring-context:jar:4.3.7.RELEASE:compile - omitted for duplicate)
|  \- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.2.RELEASE:compile
|     \- (org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile - omitted for duplicate)
+- com.datastax.cassandra:cassandra-driver-mapping:jar:3.1.4:compile
|  \- com.datastax.cassandra:cassandra-driver-core:jar:3.1.4:compile
|     +- io.netty:netty-handler:jar:4.0.37.Final:compile
|     |  +- io.netty:netty-buffer:jar:4.0.37.Final:compile
|     |  |  \- io.netty:netty-common:jar:4.0.37.Final:compile
|     |  +- io.netty:netty-transport:jar:4.0.37.Final:compile
|     |  |  \- (io.netty:netty-buffer:jar:4.0.37.Final:compile - omitted for duplicate)
|     |  \- io.netty:netty-codec:jar:4.0.37.Final:compile
|     |     \- (io.netty:netty-transport:jar:4.0.37.Final:compile - omitted for duplicate)
|     +- com.google.guava:guava:jar:16.0.1:compile
|     +- io.dropwizard.metrics:metrics-core:jar:3.1.3:compile (version managed from 3.1.2)
|     |  \- (org.slf4j:slf4j-api:jar:1.7.24:compile - version managed from 1.7.7; omitted for duplicate)
|     +- com.github.jnr:jnr-ffi:jar:2.0.7:compile
|     |  +- com.github.jnr:jffi:jar:1.2.10:compile
|     |  +- com.github.jnr:jffi:jar:native:1.2.10:runtime
|     |  +- org.ow2.asm:asm:jar:5.0.3:compile
|     |  +- org.ow2.asm:asm-commons:jar:5.0.3:compile
|     |  |  \- (org.ow2.asm:asm-tree:jar:5.0.3:compile - omitted for duplicate)
|     |  +- org.ow2.asm:asm-analysis:jar:5.0.3:compile
|     |  |  \- (org.ow2.asm:asm-tree:jar:5.0.3:compile - omitted for duplicate)
|     |  +- org.ow2.asm:asm-tree:jar:5.0.3:compile
|     |  |  \- (org.ow2.asm:asm:jar:5.0.3:compile - omitted for duplicate)
|     |  +- org.ow2.asm:asm-util:jar:5.0.3:compile
|     |  |  \- (org.ow2.asm:asm-tree:jar:5.0.3:compile - omitted for duplicate)
|     |  \- com.github.jnr:jnr-x86asm:jar:1.0.2:compile
|     \- com.github.jnr:jnr-posix:jar:3.0.27:compile
|        +- (com.github.jnr:jnr-ffi:jar:2.0.7:compile - omitted for duplicate)
|        \- com.github.jnr:jnr-constants:jar:0.9.0:compile
+- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.8.7:compile
|  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
|  +- com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile
|  \- (com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile - omitted for duplicate)
+- com.oracle.jdbc:ojdbc7:jar:12.1.0.2:compile
|  +- com.oracle.jdbc:xdb6:jar:12.1.0.2:compile
|  +- com.oracle.jdbc:orai18n:jar:12.1.0.2:compile
|  +- com.oracle.jdbc:xmlparserv2:jar:12.1.0.2:compile
|  +- com.oracle.jdbc:oraclepki:jar:12.1.0.2:compile
|  +- com.oracle.jdbc:osdt_cert:jar:12.1.0.2:compile
|  \- com.oracle.jdbc:osdt_core:jar:12.1.0.2:compile
+- com.mycompany:Pandora-json:jar:2.1.0-SNAPSHOT:compile
|  +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile - version managed from 2.5.4; omitted for duplicate)
|  +- (com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile - version managed from 2.5.4; omitted for duplicate)
|  +- com.fasterxml.jackson.datatype:jackson-datatype-joda:jar:2.8.7:compile (version managed from 2.5.4)
|  |  +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile - version managed from 2.5.4; omitted for duplicate)
|  |  +- (com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile - omitted for duplicate)
|  |  +- (com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile - version managed from 2.5.4; omitted for duplicate)
|  |  \- (joda-time:joda-time:jar:2.9.7:compile - version managed from 2.7; omitted for duplicate)
|  \- com.fasterxml.jackson.datatype:jackson-datatype-guava:jar:2.8.7:compile (version managed from 2.4.0)
|     +- (com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile - version managed from 2.5.4; omitted for duplicate)
|     +- (com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile - omitted for duplicate)
|     \- (com.google.guava:guava:jar:16.0:compile - omitted for conflict with 16.0.1)
+- javax.inject:javax.inject:jar:1:compile
+- joda-time:joda-time:jar:2.9.7:compile
+- org.projectlombok:lombok:jar:1.16.14:compile
+- org.springframework.boot:spring-boot-starter-test:jar:1.5.2.RELEASE:test
|  +- org.springframework.boot:spring-boot-test:jar:1.5.2.RELEASE:test
|  |  \- (org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:test - omitted for duplicate)
|  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.2.RELEASE:test
|  |  +- (org.springframework.boot:spring-boot-test:jar:1.5.2.RELEASE:test - omitted for duplicate)
|  |  \- (org.springframework.boot:spring-boot-autoconfigur

sim*_*imo 6

我终于设法解决了这个问题。导致它的原因是我正在为 CassandraIterable 的每个实例重新实例化 MappingCassandraConverter。

public class CassandraIterable<T> implements Iterable<T> {

    private final Iterator<Row> iterator;
    private final Class<T> entityType;

    private final MappingCassandraConverter converter = new MappingCassandraConverter ();

    ...

}
Run Code Online (Sandbox Code Playgroud)

虽然我应该创建一次并将其传递给 Iterable:

public class CassandraIterable<T> implements Iterable<T> {


    private final Iterator<Row> iterator;
    private final Class<T> entityType;

    private final MappingCassandraConverter converter;

    public CassandraIterable(Iterator<Row> iterator, Class<T> entityType, MappingCassandraConverter converter) {

        this.iterator = iterator;
        this.entityType = entityType;
        this.converter = converter;
    }

    @Override
    public Iterator<T> iterator() {
        return
                new Iterator<T>() {
                    @Override
                    public boolean hasNext() {
                        return
                                iterator.hasNext();
                    }

                    @Override
                    public T next() {
                        return
                                converter.readRow(entityType, iterator.next());
                    }
                };
    }
}
Run Code Online (Sandbox Code Playgroud)

MappingCassandraConverter 没有被垃圾收集(我假设 Cassandra 会话指向它)并且我的类的访问器可能是由转换器指向的。

我找不到泄漏的原因是泄漏没有涉及特定的类型:

在此处输入图片说明 由于泄漏,我期待看到我的一种类型在内存中重复数百万次。之所以没有发生这种情况,是因为我的代码中的问题是为 Cassandra 实体创建了大量访问器,即使只生成了一种类类型。出于这个原因,涉及泄漏的类是这样的:

在此处输入图片说明

并且它们不在按总大小排序的类型分组的列表的顶部。