Skip to content

Commit 810943c

Browse files
authored
fix(java): fix deflater memory leak (#3726)
## Why? A large number of `Fory` instances on `COMPATIBLE` mode or large amount and size object meta result in native memory leak. Native memory allocated by `libzip.so` won't be cleaned unitl next GC, making many `64M` memory chunks staying in memory space. ## What does this PR do? Explicitly close native memory allocated by libzip.so. Decompress method is fixed in #3472. Only compress method should be fixed. ## Related issues ## AI Contribution Checklist - [ ] Substantial AI assistance was used in this PR: `no` - [ ] If `yes`, I included a completed [AI Contribution Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs) in this PR description and the required `AI Usage Disclosure`. - [ ] If `yes`, my PR description includes the required `ai_review` summary and screenshot evidence of the final clean AI review results from both fresh reviewers on the current PR diff or current HEAD after the latest code changes. ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent d34c5aa commit 810943c

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

java/fory-core/src/main/java/org/apache/fory/meta/DeflaterMetaCompressor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ public byte[] compress(byte[] input, int offset, int size) {
3434
deflater.finish();
3535
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
3636
byte[] buffer = new byte[128];
37-
while (!deflater.finished()) {
38-
int compressedSize = deflater.deflate(buffer);
39-
outputStream.write(buffer, 0, compressedSize);
37+
try {
38+
while (!deflater.finished()) {
39+
int compressedSize = deflater.deflate(buffer);
40+
outputStream.write(buffer, 0, compressedSize);
41+
}
42+
} finally {
43+
deflater.end();
4044
}
4145
return outputStream.toByteArray();
4246
}

0 commit comments

Comments
 (0)