简体   繁体   中英

Possible memory leak in JDBC bulkcopy

I am writing a Spring Boot 2 application and I am using SQL Bulk Copy functionality for inserting several records in a SQL Server 2012 database. Every time I insert ~700 row i have a leak of 600 MB

I have tried Microsoft driver version 6.4.0.jre8 and 7.2.2.jre8 but anything changes. I have tried changing the Hikari Connection Pool for the tomcat one but the result is the same.

For invoking the Microsoft API I am using a wrapper framework ( https://github.com/bytefish/JSqlServerBulkInsert ) but the code is clean:

 public void saveAll(Connection connection, SQLServerBulkCopyOptions options, Stream<TEntity> entities) {
        // Create a new SQLServerBulkCopy Instance on the given Connection:
        try (SQLServerBulkCopy sqlServerBulkCopy = new SQLServerBulkCopy(connection)) {
             // Set the Options:
            sqlServerBulkCopy.setBulkCopyOptions(options);
            // The Destination Table to write to:
            sqlServerBulkCopy.setDestinationTableName(mapping.getTableDefinition().GetFullQualifiedTableName());
            // The SQL Records to insert:
            ISQLServerBulkRecord record = new SqlServerRecord<TEntity>(mapping.getColumns(), entities.iterator());
            // Finally start the Bulk Copy Process:
            sqlServerBulkCopy.writeToServer(record);
            // Handle Exceptions:
        } catch (SQLServerException e) {
            // Wrap it in a RunTimeException to provide a nice API:
            throw new RuntimeException(e);
        }
    }

When I use Eclipse memory Analyzer I have these results:

  • With Hikari:

"One instance of "com.zaxxer.hikari.pool.PoolEntry" loaded by "sun.misc.Launcher$AppClassLoader @ 0x81611758" occupies 640.619.616 (95,94 %) bytes. The memory is accumulated in one instance of "java.lang.Object[]" loaded by "".

Keywords com.zaxxer.hikari.pool.PoolEntry java.lang.Object[] sun.misc.Launcher$AppClassLoader @ 0x81611758 "

  • With Tomcat:

"One instance of "org.apache.tomcat.jdbc.pool.ConnectionPool" loaded by "sun.misc.Launcher$AppClassLoader @ 0x81614fa0" occupies 640.805.840 (95,92 %) bytes. The memory is accumulated in one instance of "java.lang.Object[]" loaded by "".

Keywords java.lang.Object[] sun.misc.Launcher$AppClassLoader @ 0x81614fa0 org.apache.tomcat.jdbc.pool.ConnectionPool"

It wasn't the driver. I was using a list inside a method and expecting to be collected after the method scope. I assigned the reference to null (previous I cleared the collection) and the GC could collect it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM