Skip to content

[performance][memory] Investigate memory consumption #3331

Open

Description

@chernser

Description

Ineffective memory allocation lead to problems with GC, memory, CPU.

There are next areas to investigate:

  • Initial start memory and idle state memory.
  • Memory used while writing operations
  • Memory used while reading operations

Activity

  1. added this to the 0.12.0 milestone on Nov 11, 2026
  2. added
    client
    on Nov 11, 2026
  3. added theissue type on Nov 11, 2026
  4. added
    jdbc
    planned_work
    on Nov 11, 2026
  5. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — investigation needed
    Planned work of epic #3324. The run gives a baseline on main: an idle Client keeps about 0.9 MB of heap, almost all of it connection buffers, and a read allocates about 230 B per row before the caller reads a value.

    Analysis: HttpAPIClientHelper.java:286-289 sizes the I/O buffers of each pooled connection with client_network_buffer_size (default 300000, ClientConfigProperties.java:213), and the pool keeps the connection after the first request. On reads, readRecord decodes each column of the row into an object in an Object[] before the caller asks for a value (AbstractBinaryFormatReader.java:186-197); a UInt64 becomes a BigInteger (BinaryStreamReader.java:186-187).

    Reproduction: complete program and output

    Allocations of the calling thread (ThreadMXBean), and used heap after System.gc().

    import com.clickhouse.client.api.Client;
    import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
    import com.clickhouse.client.api.query.QueryResponse;
    
    import java.lang.management.ManagementFactory;
    import java.util.ArrayList;
    import java.util.List;
    
    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 com.sun.management.ThreadMXBean MX =
                (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
        static final int ROWS = 1_000_000;
        static final String SELECT = "SELECT number, toString(number) FROM numbers(" + ROWS + ")";
    
        public static class Row {
            private long id;
            private String name;
            public Row() { }
            public Row(long id, String name) { this.id = id; this.name = name; }
            public long getId() { return id; }
            public void setId(long id) { this.id = id; }
            public String getName() { return name; }
            public void setName(String name) { this.name = name; }
        }
    
        static long allocated() {
            return MX.getThreadAllocatedBytes(Thread.currentThread().getId());
        }
    
        static long usedHeapAfterGc() throws InterruptedException {
            for (int i = 0; i < 3; i++) { System.gc(); Thread.sleep(100); }
            return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
        }
    
        static Client newClient() {
            return new Client.Builder().addEndpoint(URL).setUsername("default").setPassword(PASSWORD).build();
        }
    
        static Client newClient(int networkBufferSize) {
            return new Client.Builder().addEndpoint(URL).setUsername("default").setPassword(PASSWORD)
                    .setOption("client_network_buffer_size", String.valueOf(networkBufferSize)).build();
        }
    
        static void idle(String name, int networkBufferSize) throws Exception {
            List<Client> idle = new ArrayList<>();
            long beforeIdle = usedHeapAfterGc();
            long before = allocated();
            for (int i = 0; i < 20; i++) {
                Client c = networkBufferSize > 0 ? newClient(networkBufferSize) : newClient();
                c.ping();
                idle.add(c);
            }
            long idleAlloc = (allocated() - before) / 20;
            long idleRetained = (usedHeapAfterGc() - beforeIdle) / 20;
            System.out.printf("each next Client, %s (build + ping, then idle): %.1f KB allocated, %.1f KB retained after GC%n",
                    name, idleAlloc / 1e3, idleRetained / 1e3);
            for (Client c : idle) c.close();
        }
    
        static long read(Client client, boolean getters) throws Exception {
            long before = allocated();
            try (QueryResponse response = client.query(SELECT).get()) {
                ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
                long sum = 0;
                while (reader.next() != null) {
                    if (getters) {
                        sum += reader.getLong(1) + reader.getString(2).length();
                    }
                }
            }
            return (allocated() - before) / ROWS;
        }
    
        public static void main(String[] args) throws Exception {
            // start and idle memory
            long base = usedHeapAfterGc();
            long before = allocated();
            Client client = newClient();
            client.ping();
            long firstAlloc = allocated() - before;
            long firstRetained = usedHeapAfterGc() - base;
            System.out.printf("first Client (build + ping): %.1f MB allocated, %.1f MB retained after GC%n",
                    firstAlloc / 1e6, firstRetained / 1e6);
            idle("default settings", 0);
            idle("client_network_buffer_size=8192", 8192); // contrast
    
            // read: RowBinaryWithNamesAndTypes (default format), UInt64 + String
            read(client, true); // warm-up
            System.out.printf("read %d rows, next() only: %d B/row%n", ROWS, read(client, false));
            System.out.printf("read %d rows, next() + getLong + getString: %d B/row%n", ROWS, read(client, true));
    
            // write: insert of POJOs, Int64 + String
            client.execute("DROP TABLE IF EXISTS t3331").get().close();
            client.execute("CREATE TABLE t3331 (id Int64, name String) ENGINE = Memory").get().close();
            client.register(Row.class, client.getTableSchema("t3331"));
            int n = 100_000;
            List<Row> rows = new ArrayList<>(n);
            for (int i = 0; i < n; i++) rows.add(new Row(i, "name-0123456789"));
            client.insert("t3331", rows).get().close(); // warm-up
            before = allocated();
            client.insert("t3331", rows).get().close();
            System.out.printf("insert %d POJOs (Int64, String): %d B/row%n", n, (allocated() - before) / n);
            client.execute("DROP TABLE t3331").get().close();
            client.close();
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    first Client (build + ping): 11.5 MB allocated, 2.3 MB retained after GC
    each next Client, default settings (build + ping, then idle): 980.4 KB allocated, 911.1 KB retained after GC
    each next Client, client_network_buffer_size=8192 (build + ping, then idle): 105.0 KB allocated, 35.6 KB retained after GC
    read 1000000 rows, next() only: 230 B/row
    read 1000000 rows, next() + getLong + getString: 310 B/row
    insert 100000 POJOs (Int64, String): 83 B/row
    

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

  6. added
    connectivity
    data-types
    on Nov 27, 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

    clientconnectivitydata-typesjdbcperformanceplanned_work

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions