{"id":20474216,"url":"https://github.com/marschall/native-bytebuffers","last_synced_at":"2025-04-13T11:32:35.261Z","repository":{"id":45778802,"uuid":"240926179","full_name":"marschall/native-bytebuffers","owner":"marschall","description":"Native ByteBuffers for Java","archived":false,"fork":false,"pushed_at":"2022-07-16T09:00:32.000Z","size":135,"stargazers_count":6,"open_issues_count":5,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T02:43:13.687Z","etag":null,"topics":["bytebuffer","java","jni","off-heap"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marschall.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-16T16:37:16.000Z","updated_at":"2022-09-08T11:30:07.000Z","dependencies_parsed_at":"2022-08-05T12:00:12.803Z","dependency_job_id":null,"html_url":"https://github.com/marschall/native-bytebuffers","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marschall%2Fnative-bytebuffers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marschall%2Fnative-bytebuffers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marschall%2Fnative-bytebuffers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marschall%2Fnative-bytebuffers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marschall","download_url":"https://codeload.github.com/marschall/native-bytebuffers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248705569,"owners_count":21148552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bytebuffer","java","jni","off-heap"],"created_at":"2024-11-15T14:28:38.090Z","updated_at":"2025-04-13T11:32:35.236Z","avatar_url":"https://github.com/marschall.png","language":"Java","readme":"Native ByteBuffers [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.marschall/native-bytebuffers/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.marschall/native-bytebuffers) [![Javadocs](https://www.javadoc.io/badge/com.github.marschall/native-bytebuffers.svg)](https://www.javadoc.io/doc/com.github.marschall/native-bytebuffers)\n==================\n\nByteBuffers that are allocated and released directly with:\n\n* `malloc` and `free`\n* `calloc` and `free`\n* `aglined_alloc` and `free`\n* `memfd_secret` and `close`\n* `mmap` and `munmap`\n  * HugeTLB page support on Linux\n  * superpage support on macOS\n\nUsage\n-----\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.marschall\u003c/groupId\u003e\n    \u003cartifactId\u003enative-bytebuffers\u003c/artifactId\u003e\n    \u003cversion\u003e0.4.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n```java\nByteBuffer buffer = Stdlib.malloc(size);\n\ntry {\n  // do things with the buffer\n} finally {\n  Stdlib.free(buffer);\n}\n```\n\n```java\nByteBuffer buffer = Mman.mmap(length);\n\ntry {\n  // do things with the buffer\n} finally {\n  Mman.munmap(buffer);\n}\n```\n\nFor best startup performance it is recommended to extract the `.so` from the JAR and add it to a folder present in the `LD_LIBRARY_PATH` environment variable or the `java.library.path` system property. Otherwise this library will extract the `.so` to a temporary folder the first time it is called.\n\nThis library has been tested on Linux AMD64, macOS x86-64 as well as  macOS AArch64.\n\nIt should be possible to port it other Unix like *BSD.\n\nWhy would you want this?\n------------------------\n\nYou want a direct (off-heap) `java.nio.ByteBuffer` but you:\n\n* don't want to go though the slow allocation path of the JVM for `java.nio.ByteBuffer#allocateDirect` that ends up calling `java.lang.System#gc()` and `java.lang.Thread#sleep` in a loop\n* don't want to rely on the slow release mechanism of the JVM for `java.nio.ByteBuffer` that relies on garbage collection\n* don't want to use internals like `sun.misc.Unsafe`\n* don't want to rely on libffi\n\nThis project instead uses official [JNI ByteBuffer APIs](https://docs.oracle.com/en/java/javase/11/docs/specs/jni/functions.html#nio-support) and is therefore portable across JVMs.\n\n\nWhy would you not want this?\n----------------------------\n\nThis obviously brings all the issues of manual memory management to Java like:\n\n* memory leaks by not calling `free`/`munmap`\n* double free\n* use after free\n\nThese issues can crash the JVM.\n\nAs this project uses JNI a native library is required. We ship one for Linux x86-64 but for every other platform you have to build it yourself.\n\nLinks\n-----\n\n- [nosecmem](https://github.com/JonathonReinhart/nosecmem)\n- [Using Linux's memfd_secret syscall from the JVM with JEP-419](https://blog.arkey.fr/2022/05/16/linux_memfd_secret_with_jep-419/)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarschall%2Fnative-bytebuffers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarschall%2Fnative-bytebuffers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarschall%2Fnative-bytebuffers/lists"}