Giter VIP home page Giter VIP logo

java-bytebuffer-leak's Introduction

Java NIO direct ByteBuffer leak

Java's NIO API caches temporary direct ByteBuffers, which looks like a native memory leak. See my blog post for details. This repository contains a program to demonstrate the leak, a quick-and-dirty benchmark to test the performance impact, and a patch against OpenJDK 8 to remove the cache.

DirectBBLeak

This program demonstrates that the JDK "leaks" direct ByteBuffers by writing to /dev/null with multiple threads. When using heap ByteBuffers, the number of allocated direct ByteBuffers does not decrease until the threads exit, while if you use direct ByteBuffers, they decrease as usual with garbage collection. Run it with javac DirectBBLeak.java; java DirectBBLeak.

Output from Java 1.8.0_66 with the leak

=== Direct ByteBuffers ===
Before threads started:
  direct ByteBuffer count: 0 capacity:0.0 MB
All threads have written; before System.GC():
  direct ByteBuffer count: 10 capacity:1000.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 0 capacity:0.0 MB
After threads exited:
  direct ByteBuffer count: 0 capacity:0.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 0 capacity:0.0 MB

=== Heap ByteBuffers ===
Before threads started:
  direct ByteBuffer count: 0 capacity:0.0 MB
All threads have written; before System.GC():
  direct ByteBuffer count: 10 capacity:1000.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 10 capacity:1000.0 MB
After threads exited:
  direct ByteBuffer count: 10 capacity:1000.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 0 capacity:0.0 MB

Output from Java 1.8.0_66 with patched Util class (no leak)

=== Direct ByteBuffers ===
Before threads started:
  direct ByteBuffer count: 0 capacity:0.0 MB
All threads have written; before System.GC():
  direct ByteBuffer count: 10 capacity:1000.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 0 capacity:0.0 MB
After threads exited:
  direct ByteBuffer count: 0 capacity:0.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 0 capacity:0.0 MB

=== Heap ByteBuffers ===
Before threads started:
  direct ByteBuffer count: 0 capacity:0.0 MB
All threads have written; before System.GC():
  direct ByteBuffer count: 0 capacity:0.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 0 capacity:0.0 MB
After threads exited:
  direct ByteBuffer count: 0 capacity:0.0 MB
After System.GC() and Thread.sleep(100):
  direct ByteBuffer count: 0 capacity:0.0 MB

BBWritePerfTest

Measures the performance of writing to /dev/null in a loop. This can be used to measure the performance impact of changes to this cache. Removing the cache completely does hurt performance, particularly for small I/O calls. Run it with javac BBWritePerfTest.java; java BBWritePerfTest.

remove-bb-cache.diff

This patch removes the direct ByteBuffer cache completely. See my blog post for how to compile and replace the affected class.

java-bytebuffer-leak's People

Contributors

evanj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.