nemalloc is a research-oriented memory allocator that explores how machine learning (specifically reinforcement learning with Q-tables) and SIMD instructions can be combined to improve memory allocation performance and reduce fragmentation.
Note: This is an experimental project. It is not production-ready, but serves as a testing sandbox for AI-driven system optimizations.
- AI-Assisted Allocation: Uses a Q-table (trained via reinforcement learning) to predict and select optimal allocation strategies based on runtime patterns.
- SIMD Optimizations: Leverages vectorized instructions for faster memory operations (copying, zeroing, etc.).
- Compaction: Includes a compaction mechanism to reduce fragmentation by defragmenting allocated memory regions.
- Shared Library: Can be preloaded (LD_PRELOAD) to replace malloc/free in existing applications without recompilation.
- Training Mode: Includes a training harness (qtable_training.cpp) to adapt the Q-table to specific workloads.
nemalloc/
│
├── source/ # Core implementation
│ ├── include/ # Header files
│ │ ├── compaction.hpp
│ │ ├── helper.hpp
│ │ ├── memory.hpp
│ │ ├── nemalloc.hpp
│ │ ├── qtable.hpp
│ │ └── types.hpp
│ ├── compaction.cpp # Memory compaction logic
│ ├── globals.cpp # Global state and configuration
│ ├── main.cpp # Standalone test/demo executable
│ ├── memory.cpp # Core memory management (malloc/free)
│ ├── nemalloc.cpp # Main allocator interface and AI integration
│ └── qtable_training.cpp # Reinforcement learning training harness
├── .gitignore
├── LICENSE # License terms
├── Makefile # Build system
├── qtable.bin # Pre-trained Q-table
└── README.md # Project documentation
- Build from Source: git clone https://github.com/herenturker/nemalloc.git cd nemalloc make so Output:
- libnemalloc.so (Shared library)
- Preload into Existing Programs (LD_PRELOAD): LD_PRELOAD=/path/to/libnemalloc.so ./your_program Example with ls: LD_PRELOAD=./libnemalloc.so ls -la
- Run Test Suite: ./nemalloc_test
- Train Custom Q-Table: ./main The Q-Table will be automatically trained if no qtable.bin is present.
Please use -lnemalloc when linking.
#include "nemalloc.hpp"
int main()
{
handle_t h = nemalloc(100);
void* p = get_ptr(h);
nefree(h);
return 0;
}
- C++17 compiler
- GNU Make
- Linux x86_64 (AVX2 recommended)
Refer to the LICENSE file in the repository for terms. Inspired by ML-driven systems research.