Skip to content

Commit c8c593e

Browse files
authored
Fix memory leak of direct memory. (#3983)
Descriptions of the changes in this PR: ### Motivation When open a non-exist file for DirectReader, it will throw an exception. The ByteBuf won't release. When close DirectWriter, if the close failed, the ByteBuf also won't release.
1 parent 7f64246 commit c8c593e

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/directentrylogger/DirectReader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ class DirectReader implements LogReader {
5656
this.filename = filename;
5757
this.maxSaneEntrySize = maxSaneEntrySize;
5858
this.readBlockStats = readBlockStats;
59-
60-
nativeBuffer = new Buffer(nativeIO, bufferSize);
6159
closed = false;
6260

6361
try {
@@ -71,6 +69,7 @@ class DirectReader implements LogReader {
7169
.kv("errno", ne.getErrno()).toString());
7270
}
7371
refreshMaxOffset();
72+
nativeBuffer = new Buffer(nativeIO, bufferSize);
7473
}
7574

7675
@Override

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/directentrylogger/DirectWriter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,11 @@ public void close() throws IOException {
233233
throw new IOException(exMsg(ne.getMessage())
234234
.kv("file", filename)
235235
.kv("errno", ne.getErrno()).toString());
236-
}
237-
synchronized (bufferLock) {
238-
bufferPool.release(nativeBuffer);
239-
nativeBuffer = null;
236+
} finally {
237+
synchronized (bufferLock) {
238+
bufferPool.release(nativeBuffer);
239+
nativeBuffer = null;
240+
}
240241
}
241242
}
242243

0 commit comments

Comments
 (0)