Giter Club home page Giter Club logo

leveldb-jna's Introduction

Build Status Maven Central

Java JNA (not JNI) adapter to LevelDB

leveldb-jna is Java JNA adapter to LevelDB key-value database.

Supported platforms

  • OS X
  • Linux x86/x64
  • Windows x86/x64 (backport from Bitcoin project)

Examples

See examples of usage here.

Demo

leveldb-jna is used in these applications and libraries:

  • Keylord - cross-platform GUI application for key-value databases.

Build

Build process described here.

leveldb-jna's People

Contributors

maxd avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

leveldb-jna's Issues

Trying levedb jna

Hey Max , (@maxd )

I want to try your library bridge ,
did you have any good testing
over this way of working with leveldb

I am looking for good alternative for
pure java leveldb.

Thanks
Roman.

Nasty Memory Leak

I know this is an old project with little activity, but I'm leaving this issue here as a warning to future users. There is a massive memory leak in this wrapper.

Basic Info

I'm using Mojang's LevelDB library, which is based on the latest version of LevelDB 1.18, but supports Windows and adds zlib compression. On both Windows and Ubuntu 14.04 (only two platforms I have to test with), I encounter a very large memory leak where the number of objects allocated outside the Java heap continues to grow until eventually the program uses up all system RAM. I spent 2 days looking into this issue until I finally figured out it was due to my complete ignorance of what this JNA wrapper does behind the scenes.

The Problem

LevelDB.get is the main cause of the memory leak, though there may be smaller leaks littered throughout the wrapper. The native LevelDB library returns an malloc'd byte array to the wrapper, which means it's up to the wrapper to free the byte array. However, this wrapper's LevelDB.get method returns a copy of the native byte array allocated by the LevelDB library, but doesn't free the original native byte array. This means the original byte array malloc'd by the native LevelDB library is never freed and persists outside the Java heap, completely invisible to the Java garbage collector.

JNA will not automatically free the byte arrays returned by the native LevelDB library or pointed to by a PointerToReference. These arrays must be freed manually.

Solutions

For future users, fork this repo and change LevelDB.get using one of the following solutions:


Solution 1

Modify the LevelDB.get function to clear the data being pointed to before returning the copied byte array, like so:

byte[] returnValue = result != null ? result.getPointer().getByteArray(0, (int) resultLength) : null;
if(result != null)
    LevelDBNative.leveldb_free(result.getPointer());
return returnValue;

This option also has lots of overhead as Java still has to copy the original byte array to the Java heap.


Solution 2

Modify the LevelDB.get function to return a ByteBuffer mapped to the original byte data using:

result.getPointer().getByteBuffer(0, (int) resultLength)

This option will have a little less overhead as Java doesn't have to copy the original byte array; however, the ByteBuffer returned is a DirectByteBuffer, which may not be ideal for some applications and use-case scenarios.

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.