A Kotlin library providing a simple, high-performance way to use off-heap native memory in JVM applications.

Overview

native_memory_allocator

A library which uses sun.misc.Unsafe to allocate off-heap native memory.

Motivation

The goal of this project is to provide a simple way to allow JVM applications to store many gigabytes of data in local memory without garbage collection pauses and overhead.

This project provides the following components:

  • NativeMemoryAllocator
    • Allocates a configurable-size block of native memory on creation
    • Breaks this block into pages of configurable size and maintains a list of free pages
    • Supports operations to allocate, resize, and free native memory buffers similar to the malloc, realloc, and free functions in C
    • Advantages of this design:
      • All allocate, resize, and free operations after startup are very fast because they do not do native-memory allocation or free operations
      • Fragmentation is not an issue because NativeMemoryBuffer has no requirement for memory pages to be contiguous
  • Buffer
    • Supports OnHeapMemoryBuffer as a normal on-heap byte array
    • Supports NativeMemoryBuffer as a list of native memory pages allocated by NativeMemoryAllocator
    • Supports operations to copy data between OnHeap and Native buffers efficiently
  • NativeMemoryMap
    • Simple, high-level Map interface supporting get and put operations
    • Manages native memory automatically as entries are added and deleted using NativeMemoryAllocator
    • Supports configurable map backends
  • Metrics
    • Metrics reporter classes exist for NativeMemoryAllocator and NativeMemoryMap using Micrometer

Example Usage

See the examples directory for complete runnable example applications.

Basic steps to use NativeMemoryAllocator:

Add the following repository to maven or gradle build:

https://maven.pkg.github.com/target/native_memory_allocator

Add com.target:native_memory_allocator dependency to maven or gradle build - see releases page for current version.

Define a map value object.

data class CacheObject(
    val s: String,
)

Define a NativeMemoryMapSerializer to convert the value object to and from byte buffers. This serializer can use any form of serialization (e.g. Google Flatbuffers, Google Protocol Buffers, etc). The serializer will be invoked on every get and put operation to convert between a byte buffer and a normal object.

private class CacheObjectSerializer : NativeMemoryMapSerializer<CacheObject> {

    override fun deserializeFromOnHeapMemoryBuffer(onHeapMemoryBuffer: OnHeapMemoryBuffer): CacheObject {
        return CacheObject(
            s = String(onHeapMemoryBuffer.toTrimmedArray()),
        )
    }

    override fun serializeToByteArray(value: CacheObject): ByteArray {
        return value.s.toByteArray()
    }

}

Define a NativeMemoryAllocator with a certain page size and native memory size. In this example we use a 20GB native memory size and a 4KB page size.

The NativeMemoryAllocator will do a single allocation of all native memory when it is constructed. This is intended to be used for the life of the application.

val nativeMemoryAllocator = NativeMemoryAllocatorBuilder(
    pageSizeBytes = 4_096,
    nativeMemorySizeBytes = (20L * 1_024L * 1_024L * 1_024L),
).build()

Define a NativeMemoryMap specifying the valueSerializer, nativeMemoryAllocator, and backend type to use.

Multiple NativeMemoryMaps may share a single NativeMemoryAllocator.

val nativeMemoryMap = NativeMemoryMapBuilder<Int, CacheObject>(
    valueSerializer = CacheObjectSerializer(),
    nativeMemoryAllocator = nativeMemoryAllocator,
    backend = NativeMemoryMapBackend.CONCURRENT_HASH_MAP,
).build()

Put a value into the map. The key will be stored as a normal object on-heap memory. The value will be serialized and stored in off-heap memory allocated by NativeMemoryAllocator.

nativeMemoryMap.put(
    key = 1,
    value = CacheObject(
        s = "test",
    ),
)

Get a value from the map. The value will be fetched from native memory, copied into an OnHeapBuffer, deserialized, and returned as a nullable value to indicate presence or absence in the map.

val value = nativeMemoryMap.get(key = 1)

Class Diagram

NMAComponents

Generating KDoc HTML documentation

Kotlin classes in this library are documented with KDoc style comments.

The Dokka plugin is present in build.gradle.kts.

To generate HTML documentation use the following command:

./gradlew -PdokkaEnabled=true dokkaHtml

Open build/dokka/html/index.html to view generated documentation.

You might also like...
Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.
Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Backend Project to support providing help to refugees

Project Project to implement the matching of guests and hosts, with special attention to verification of matches. Get it, run it: In order to build th

Display code with syntax highlighting :sparkles: in native way.
Display code with syntax highlighting :sparkles: in native way.

CodeView (Android) CodeView helps to show code content with syntax highlighting in native way. Description CodeView contains 3 core parts to implement

Kamper - a small KMM/KMP library that provides performance monitoring for your app.
Kamper - a small KMM/KMP library that provides performance monitoring for your app.

🎯 Kamper Kamper is a KMP/KMM library that implements a unified way to track application performances. The solution is based on plugin design patterns

🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)
🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)

Haru 🚟 Lightweight, and simple scheduling library made for Kotlin (JVM) Why did you build this? I built this library as a personal usage library to h

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.
An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile. 项目架构主要分为原生系统层、Android/iOS业务SDK层、KMM SDK层、KMM业务逻辑SDK层、iOS sdkfra

The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

kinstall is an easy way to install gradle-based command-line kotlin projects that use the application plugin.

kinstall kinstall is an easy way to install gradle-based command-line kotlin projects that use the application plugin. use First, install kinstall its

A property/method accessor library for the JVM, written in Kotlin

unlok - unlock your JVM a property/method accessor library for the JVM, written in Kotlin. how to import you can import unlok from maven central just

Releases(2.2.0)
Owner
Target
Target's official GitHub organization
Target
A high-performance fork of Paper/Airplane designed for large servers.

Pufferfish A highly optimized Paper/Airplane fork designed for large servers requiring both maximum performance, stability, and "enterprise" features.

Pufferfish Studios LLC 399 Jan 7, 2023
Minecraft 1.18.2 Backport of Petal, a performance-oriented fork of Purpur intended to increase performance for entity-heavy servers by implementing multi-threaded and asynchronous improvements.

Sakura Performance Minecraft JAR Sakura is a performance-oriented fork of Purpur intended to increase performance for entity-heavy servers by implemen

etil.sol 14 Nov 23, 2022
A simplified interface for interacting with in-memory cache implementations on the JVM

This library provides a simplified interface for interacting with in-memory cache implementations on the JVM. Think: "SLF4J but for caching"

null 5 Nov 29, 2022
A plugin for Termux to use native Android GUI components from CLI applications.

Termux:GUI This is a plugin for Termux that enables command line programs to use the native android GUI. In the examples directory you can find demo v

null 342 Dec 23, 2022
Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

Cash App 1.5k Dec 30, 2022
🎑 Up to date IANA timezone database library for Kotlin (JVM, JS, Native)

?? IANA Timezone Library for Kotlin Multiplatform Up to date IANA timezone database library for Kotlin (JVM, JS, Native) Usage import org.noelware.ian

Noelware 3 Jun 18, 2022
A Template for a Github Actions Pipeline for building and publishing Gradle-JVM Applications

github-actions-cd-template-jvm A Template for a Github Actions Pipeline for building and publishing Gradle-JVM Applications It build a executable shad

Raphael Panic 0 Dec 5, 2021
Yet Another Native Loader for the JVM.

yanl - yet another native loader Yet another Native library extractor/loader for the JVM, written in Kotlin. why other libraries simply don't fit my n

Stardust Enterprises 5 Dec 23, 2022
Spigot-Plugin message providing system written in Kotlin

teller Spigot-Plugin message providing system written in Kotlin Usage Create an instance of PropertiesMessageProvider using the Constructor with an in

Luca Zieserl 2 Jan 16, 2022
A sample app showing different kind of memory leaks and their solutions

MemoryLeakApp A sample app showing different kind of memory leaks and their solutions. Make sure to check the source code to understand the concept of

Moradious 11 Oct 28, 2022