Skip to content

addBatch method cause Java heap memory char array grows too fast #1873

Description

@wangbing505

image
The scenario involves a large number of calls to the addBatch method, Then use jmap to view heap memory,has lots of char[],although they are all unreachable objects that can be GC dropped.But it will lead to frequent GC and high CPU usage. How to solve it

Activity

  1. added
    legacy
    and removed on Oct 21, 2024
  2. chernser commented on Oct 21, 2024

    @chernser
    Contributor

    Hi @wangbing505

    • What is the ClickHouse jdbc version do you use?
    • Is it a requirement to use JDBC?
  3. wangbing505 commented on Oct 23, 2024

    @wangbing505
    Author

    Hi @chernser

    1. ClickHouse jdbc version is 0.2.6,Can a higher version solve this problem?
    2. yes,or Is there any other way to write data to clickhouse ?
  4. chernser commented on Oct 23, 2024

    @chernser
    Contributor

    @wangbing505

    1. Yes, please, update to the latest 0.7.0 version. I do not see such code in it and everything should work fine.
    2. Yes, you may use java client without JDBC - https://clickhouse.com/docs/en/integrations/java/client-v2

    Please let me know how the update goes. Thanks!

  5. wangbing505 commented on Oct 24, 2024

    @wangbing505
    Author

    @chernser
    Thank you for your answer,I will try using them and synchronize the results

  6. chernser commented on Nov 4, 2024

    @chernser
    Contributor

    @wangbing505 as the issue solved?

  7. wangbing505 commented on Nov 5, 2024

    @wangbing505
    Author

    @chernser hello, I have been delayed by other things and will try it next week

  8. wangbing505 commented on Nov 7, 2024

    @wangbing505
    Author

    @chernser Thank you, I tried it, Char [] has decreased,But there are other memory uses as well,maybe it's not a problem with the client anymore

  9. added
    jdbc
    on Jan 9, 2025
  10. chernser commented on Jan 9, 2025

    @chernser
    Contributor

    Need to verify how jdbc-v2 handles it.

  11. chernser commented on Nov 14, 2025

    @chernser
    Contributor

    We might need to stream big SQL requests like we do with text formats.
    Will prioritize this work.

  12. added this to the 0.12.0 milestone on Jul 22, 2026
  13. removed this from the 0.12.0 milestone on Nov 10, 2026
  14. removed
    legacy
    on Nov 26, 2026
  15. polyglotAI-bot commented on Nov 28, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Close — duplicate of #3332
    #3332 is the planned work for the same SQL-text batch allocations in jdbc-v2, with the code path. This report adds no other case.

    • Category: Performance
    • Classification: impact: Performance · area: Queries (batch inserts)
    • Status on main: Reproduced on main — a batch of 100,000 rows allocates 292 B/row in set* + addBatch and 160 B/row in executeBatch.
    • Breaking change: No — can be done without a break.
    • Duplicates / related: Related: [performance][jdbc] Reduce memory allocation in PreparedStatement #3333 — reduces the allocations of the same set* calls.
    • Workaround: For a simple INSERT INTO t VALUES (?, ?), the beta property beta.row_binary_for_simple_insert=true sends RowBinary: 177 + 24 B/row in the same run.

    Analysis: The report used 0.2.6 and 0.7.0, before jdbc-v2 became the default driver (0.8.0). jdbc-v2 on main also builds the batch as SQL text: each set* call makes a String (encodeObject, PreparedStatementImpl.java:764), addBatch copies the VALUES tuple of the row into a new StringBuilder (PreparedStatementImpl.java:322-330), and executeBatch joins all tuples into one more StringBuilder and String (PreparedStatementImpl.java:368-377).

    Reproduction: complete program and output
    import java.lang.management.ManagementFactory;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
        static final int ROWS = 100_000;
        static final com.sun.management.ThreadMXBean MX =
                (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
    
        static long allocated() {
            return MX.getThreadAllocatedBytes(Thread.currentThread().getId());
        }
    
        static Connection connect(boolean rowBinary) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            props.setProperty("beta.row_binary_for_simple_insert", String.valueOf(rowBinary));
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
            return DriverManager.getConnection(jdbcUrl, props);
        }
    
        // One batch of ROWS rows; prints the bytes that the thread allocates per row.
        static void run(boolean rowBinary, boolean print) throws Exception {
            try (Connection conn = connect(rowBinary);
                 PreparedStatement ps = conn.prepareStatement("INSERT INTO t1873 VALUES (?, ?)")) {
                long a0 = allocated();
                for (int i = 0; i < ROWS; i++) {
                    ps.setLong(1, i);
                    ps.setString(2, "value-" + i);
                    ps.addBatch();
                }
                long a1 = allocated();
                ps.executeBatch();
                long a2 = allocated();
                if (print) {
                    System.out.printf("%s (%s): set*+addBatch %d B/row, executeBatch %d B/row%n",
                            rowBinary ? "beta.row_binary_for_simple_insert=true" : "default",
                            ps.getClass().getSimpleName(), (a1 - a0) / ROWS, (a2 - a1) / ROWS);
                }
            }
        }
    
        public static void main(String[] args) throws Exception {
            try (Connection conn = connect(false); Statement s = conn.createStatement()) {
                s.execute("DROP TABLE IF EXISTS t1873");
                s.execute("CREATE TABLE t1873 (id Int64, s String) ENGINE = Null");
            }
            for (boolean rowBinary : new boolean[] {false, true}) {
                run(rowBinary, false); // warm-up (JIT)
                run(rowBinary, true);
            }
            try (Connection conn = connect(false); Statement s = conn.createStatement()) {
                s.execute("DROP TABLE t1873");
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.4.11:

    default (PreparedStatementImpl): set*+addBatch 292 B/row, executeBatch 160 B/row
    beta.row_binary_for_simple_insert=true (WriterStatementImpl): set*+addBatch 177 B/row, executeBatch 24 B/row
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  16. added
    queries
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicatejdbcperformancequeriesquestion

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions