{"id":28098623,"url":"https://github.com/target/native_memory_allocator","last_synced_at":"2026-02-05T19:00:28.618Z","repository":{"id":38085169,"uuid":"489390330","full_name":"target/native_memory_allocator","owner":"target","description":"O(1) malloc for off-heap storage in Kotlin, and a NativeMemoryMap based on Caffeine.","archived":false,"fork":false,"pushed_at":"2026-01-30T16:58:18.000Z","size":720,"stargazers_count":17,"open_issues_count":0,"forks_count":2,"subscribers_count":11,"default_branch":"main","last_synced_at":"2026-01-31T09:47:12.239Z","etag":null,"topics":["caching","flatbuffers","jvm","kotlin","nativememory","offheap","serialization"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/target.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-05-06T14:44:19.000Z","updated_at":"2026-01-30T16:58:07.000Z","dependencies_parsed_at":"2025-08-26T15:19:11.666Z","dependency_job_id":"9c698076-4c97-4f5a-aad5-38d65e2e571a","html_url":"https://github.com/target/native_memory_allocator","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/target/native_memory_allocator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/target%2Fnative_memory_allocator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/target%2Fnative_memory_allocator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/target%2Fnative_memory_allocator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/target%2Fnative_memory_allocator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/target","download_url":"https://codeload.github.com/target/native_memory_allocator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/target%2Fnative_memory_allocator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29130088,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T18:55:47.139Z","status":"ssl_error","status_checked_at":"2026-02-05T18:55:04.010Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["caching","flatbuffers","jvm","kotlin","nativememory","offheap","serialization"],"created_at":"2025-05-13T17:58:27.645Z","updated_at":"2026-02-05T19:00:28.612Z","avatar_url":"https://github.com/target.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# native_memory_allocator\n\nO(1) malloc for off-heap storage in Kotlin, and a NativeMemoryMap based on [Caffeine](https://github.com/ben-manes/caffeine).\n\n## Java Version\n\nProject is built with Java 17.  Works at runtime with any Java \u003e= 17.\n\nUsed in production and regularly tested with Java 17 and 21 LTS versions.\n\n## Motivation\n\nThe goal of this project is to provide a simple way to allow JVM applications to store many gigabytes of data in local\nmemory without garbage collection pauses and overhead.\n\nThis project provides the following components:\n\n* NativeMemoryAllocator\n    * Allocates a configurable-size block of native memory on creation\n    * Breaks this block into pages of configurable size and maintains a list of free pages\n    * Supports operations to allocate, resize, and free native memory buffers similar to the malloc, realloc, and free\n      functions in C\n    * Advantages of this design:\n        * All allocate, resize, and free operations after startup are very fast because they do not do native-memory\n          allocation or free operations\n        * Fragmentation is not an issue because NativeMemoryBuffer has no requirement for memory pages to be contiguous\n* Buffer\n    * Supports OnHeapMemoryBuffer as a normal on-heap byte array\n    * Supports NativeMemoryBuffer as a list of native memory pages allocated by NativeMemoryAllocator\n    * Supports operations to copy data between OnHeap and Native buffers efficiently\n* NativeMemoryMap\n    * Simple, high-level Map interface supporting get and put operations\n    * Manages native memory automatically as entries are added and deleted using NativeMemoryAllocator\n    * Supports configurable map backends\n        * java.util.concurrent.ConcurrentHashMap\n        * Caffiene cache allowing use of LRU and TTL eviction algorithms https://github.com/ben-manes/caffeine\n* Metrics\n    * Metrics reporter classes exist for NativeMemoryAllocator and NativeMemoryMap using Micrometer\n\n\n## Benchmarks\n\nSee the [`benchmarks`](https://github.com/target/native_memory_allocator/tree/main/benchmarks) directory for JMH benchmarks.\n\nBenchmark results:\n* [GCP VM](https://github.com/target/native_memory_allocator/wiki/Benchmark-results-in-GCP-VM)\n* [Local Laptop](https://github.com/target/native_memory_allocator/wiki/Benchmark-results-in-local-laptop)\n\n## Using as a maven or gradle dependency\n\nArtifact is deployed to public [Maven Central repository](https://repo1.maven.org/maven2/com/target/native-memory-allocator/)\n\nTo use in a maven or gradle build, use:\n```\ncom.target:native-memory-allocator:\u003cVERSION\u003e\n```\n\nSee [releases page](https://github.com/target/native_memory_allocator/releases) for latest version.\n\n## Example Usage\n\nSee the `examples` directory for complete runnable example applications.\n\nBasic steps to use NativeMemoryAllocator:\n\nDefine a map value object.\n\n```\ndata class CacheObject(\n    val s: String,\n)\n```\n\nDefine a `NativeMemoryMapSerializer` to convert the value object to and from byte buffers. This serializer can use any\nform of serialization (e.g. Google Flatbuffers, Google Protocol Buffers, etc). The serializer will be invoked on every\nget and put operation to convert between a byte buffer and a normal object.\n\n```\nprivate class CacheObjectSerializer : NativeMemoryMapSerializer\u003cCacheObject\u003e {\n\n    override fun deserializeFromOnHeapMemoryBuffer(onHeapMemoryBuffer: OnHeapMemoryBuffer): CacheObject {\n        return CacheObject(\n            s = String(onHeapMemoryBuffer.toTrimmedArray()),\n        )\n    }\n\n    override fun serializeToByteArray(value: CacheObject): ByteArray {\n        return value.s.toByteArray()\n    }\n\n}\n```\n\nDefine a NativeMemoryAllocator with a certain page size and native memory size. In this example we use a 20GB native\nmemory size and a 4KB page size.\n\nThe NativeMemoryAllocator will do a single allocation of all native memory when it is constructed. This is intended to\nbe used for the life of the application.\n\n```\nval nativeMemoryAllocator = NativeMemoryAllocatorBuilder(\n    pageSizeBytes = 4_096,\n    nativeMemorySizeBytes = (20L * 1_024L * 1_024L * 1_024L),\n).build()\n```\n\nDefine a NativeMemoryMap specifying the valueSerializer, nativeMemoryAllocator, and backend type to use.\n\nMultiple NativeMemoryMaps may share a single NativeMemoryAllocator.\n\n```\nval nativeMemoryMap = NativeMemoryMapBuilder\u003cInt, CacheObject\u003e(\n    valueSerializer = CacheObjectSerializer(),\n    nativeMemoryAllocator = nativeMemoryAllocator,\n    backend = NativeMemoryMapBackend.CONCURRENT_HASH_MAP,\n).build()\n```\n\nPut a value into the map. The key will be stored as a normal object on-heap memory. The value will be serialized and\nstored in off-heap memory allocated by NativeMemoryAllocator.\n\n```\nnativeMemoryMap.put(\n    key = 1,\n    value = CacheObject(\n        s = \"test\",\n    ),\n)\n```\n\nGet a value from the map. The value will be fetched from native memory, copied into an OnHeapBuffer, deserialized, and\nreturned as a nullable value to indicate presence or absence in the map.\n\n```\nval value = nativeMemoryMap.get(key = 1)\n```\n\n## Class Diagram\n\n![NMAComponents](NMAComponents.png)\n\n## Generating KDoc HTML documentation\n\nKotlin classes in this library are documented with [KDoc](https://kotlinlang.org/docs/kotlin-doc.html) style comments.\n\nThe [Dokka](https://github.com/Kotlin/dokka) plugin is present in `build.gradle.kts`.\n\nTo generate HTML documentation use the following command:\n\n```\n./gradlew -PdokkaEnabled=true dokkaHtml\n```\n\nOpen `build/dokka/html/index.html` to view generated documentation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarget%2Fnative_memory_allocator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftarget%2Fnative_memory_allocator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarget%2Fnative_memory_allocator/lists"}