Skip to content

Commit

Permalink
[branch-2.0][fix](jdbc catalog) Fix Memory Leak by Enabling Weak Refe…
Browse files Browse the repository at this point in the history
…rences in HikariCP (#39835)

pick (#39582)

This PR addresses a memory leak issue caused by FastList objects in
HikariCP being retained by ThreadLocal variables, which are not easily
garbage collected in long-running JNI threads. To mitigate this, a
system property com.zaxxer.hikari.useWeakReferences is set to true,
ensuring that WeakReference is used for ThreadLocal objects, allowing
the garbage collector to reclaim memory more effectively. Even though
setting this will affect some performance, solving resource leaks is
relatively more important
Performance difference before and after setting
Before setting:
10 concurrency 0.02-0.05
100 concurrency 0.18-0.4
After setting:
10 concurrency 0.02-0.07
100 concurrency 0.18-0.7
  • Loading branch information
zy-kkk authored Aug 23, 2024
1 parent e02f583 commit 0329adf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public Map<String, HikariDataSource> getSourcesMap() {
}

public void setCleanupInterval(long interval) {
this.cleanupInterval = interval * 1000L;
restartCleanupTask();
if (this.cleanupInterval != interval * 1000L) {
this.cleanupInterval = interval * 1000L;
restartCleanupTask();
}
}

private synchronized void restartCleanupTask() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public JdbcExecutor(byte[] thriftParams) throws Exception {
.setConnectionPoolMaxLifeTime(request.connection_pool_max_life_time)
.setConnectionPoolKeepAlive(request.connection_pool_keep_alive);
JdbcDataSource.getDataSource().setCleanupInterval(request.connection_pool_cache_clear_time);
System.setProperty("com.zaxxer.hikari.useWeakReferences", "true");
init(config, request.statement);
}

Expand Down

0 comments on commit 0329adf

Please sign in to comment.