Skip to content

neoionet/netty-allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Netty mimalloc allocator

License Java Version Netty Version Virtual Thread Friendly

A high-performance Java port of the mimalloc allocator, tailored for Netty.


🚀 Key Features

  • Mimalloc Powered: Leverages mimalloc's advanced allocation strategies (free lists, local shards).
  • Tailored for Netty: Specifically designed to handle Netty's ByteBuf allocation with minimal overhead.
  • High Throughput: Optimized for multi-threaded network environments to ensure high and stable performance.
  • Virtual-Threads Friendly: Optimized for Project Loom, preventing the memory explosion typically caused by massive ThreadLocal usage in virtual thread scenarios.

🛠 Requirements

Requirement Minimum Version
Java (JDK) 1.8 or higher
Netty 4.2.10.Final or newer

📖 How to use

1. Maven dependencies:

Add the following dependencies to your pom.xml:

    <dependencies>
        <dependency>
            <groupId>io.github.neoionet</groupId>
            <artifactId>mimalloc</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-common</artifactId>
            <version>4.2.10.Final</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-buffer</artifactId>
            <version>4.2.10.Final</version>
        </dependency>
    </dependencies>

If you want to use the mimalloc allocator within your server or client transport, ensure the netty-transport dependency is included in your project as well:

    <dependencies>
       <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport</artifactId>
            <version>4.2.10.Final</version>
        </dependency>
    </dependencies>

2. Quick start

2.1. Initialize Allocator:

// Create an instance of the mimalloc-based allocator.
ByteBufAllocator miMallocAllocator = new MiByteBufAllocator();

2.2. Simple allocation:

ByteBuf buf = miMallocAllocator.directBuffer();
buf.writeLong(123);
System.out.println(buf.readLong()); // print 123.
buf.release();

2.3. Apply to Server:

ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
 .channel(NioServerSocketChannel.class)
 .option(ChannelOption.ALLOCATOR, miMallocAllocator) // Set the mimalloc allocator.
 .childOption(ChannelOption.ALLOCATOR, miMallocAllocator) // Set the mimalloc allocator for child.
 ...

2.4. Apply to Client:

Bootstrap b = new Bootstrap();
b.group(group)
 .channel(NioSocketChannel.class)
 .option(ChannelOption.ALLOCATOR, miMallocAllocator) // Set the mimalloc allocator.
 ...

🌟 Acknowledgments

This project would not be possible without the following open-source works:

  • mimalloc - A compact general purpose allocator with excellent performance.
  • Netty - An event-driven asynchronous network application framework.

About

A high-performance Java port of the mimalloc memory allocator, specifically tailored and optimized for Netty.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages