This directory contains organized examples demonstrating various use cases of ZiggyAlloc.
The examples are organized into the following categories:
Fundamental usage examples for getting started with ZiggyAlloc.
01-SimpleAllocation.cs- Basic memory allocation and usage
Advanced features and patterns.
DeferPatterns.cs- Advanced defer patterns and error handlingMemoryLeakDetection.cs- Memory leak detection with DebugMemoryAllocator
Examples specific to different allocator types.
HybridAllocatorExample.cs- Demonstrates the enhanced HybridAllocator
Performance optimization techniques and benchmarks.
UnmanagedMemoryPoolExample.cs- Shows how to use memory pools for improved performance
Real-world application scenarios.
ImageProcessingExample.cs- Image processing without GC pressure
cd examples
dotnet run -- basic
dotnet run -- allocators
dotnet run -- performance
dotnet run -- realworldcd examples/01-Basic
dotnet run
cd examples/03-Allocators
dotnet runcd examples
dotnet run
# Then follow the prompts to run specific example categoriesDifferent examples showcase various performance characteristics:
| Example Category | Performance Benefit | GC Pressure | Use Case |
|---|---|---|---|
| Basic Allocation | Moderate | ❌ Eliminated | Learning fundamentals |
| Advanced Patterns | High | ❌ Eliminated | Complex memory management |
| Allocator Types | Very High | ❌ Eliminated | Choosing right allocator |
| Performance Optimization | Maximum | ❌ Eliminated | High-performance scenarios |
| Real-World Applications | Contextual | ❌ Eliminated | Production use cases |
These examples demonstrate fundamental ZiggyAlloc concepts:
- Simple memory allocation and automatic cleanup
- Using
usingstatements for RAII-style memory management - Converting buffers to
Span<T>for high-performance operations
These examples show the different allocator types:
Automatically chooses between managed and unmanaged allocation based on size and type
var systemAllocator = new SystemMemoryAllocator();
var hybridAllocator = new HybridAllocator(systemAllocator);
// Small allocations may use managed arrays for better performance
using var smallBuffer = hybridAllocator.Allocate<int>(100);
// Large allocations will use unmanaged memory to avoid GC pressure
using var largeBuffer = hybridAllocator.Allocate<int>(10000);Direct system memory allocation
Arena-style allocation with automatic cleanup
Leak detection and debugging
Reusing buffers to reduce allocation overhead
Specialized allocator for large memory blocks with pooling and alignment
var systemAllocator = new SystemMemoryAllocator();
using var largeBlockAllocator = new LargeBlockAllocator(systemAllocator);
// Large allocations automatically benefit from pooling and alignment
using var largeBuffer = largeBlockAllocator.Allocate<byte>(1024 * 1024); // 1MB
// Memory is automatically pooled for reuse
using var anotherBuffer = largeBlockAllocator.Allocate<byte>(1024 * 1024); // Reuses pooled memoryThese examples demonstrate performance optimization techniques:
// Without pooling - each allocation calls into the OS
var allocator = new SystemMemoryAllocator();
for (int i = 0; i < 1000; i++)
{
using var buffer = allocator.Allocate<byte>(1024); // System call each time
// Process buffer...
}
// With pooling - first allocation per size calls OS, subsequent allocations reuse
using var pool = new UnmanagedMemoryPool(allocator);
for (int i = 0; i < 1000; i++)
{
using var buffer = pool.Allocate<byte>(1024); // Reuses pooled buffer
// Process buffer...
}- Large allocations using unmanaged memory
- Efficient buffer operations using
Span<T>
These examples show practical applications:
Process large images without triggering garbage collection:
using var allocator = new SystemMemoryAllocator();
using var imageBuffer = allocator.Allocate<byte>(width * height * 4); // RGBA
// Process image data without GC pressure
Span<byte> imageData = imageBuffer;
for (int i = 0; i < imageData.Length; i += 4)
{
// Manipulate pixel data
imageData[i] = 255; // Red
imageData[i + 1] = 128; // Green
imageData[i + 2] = 0; // Blue
imageData[i + 3] = 255; // Alpha
}Working with native APIs and raw pointers
Large datasets with mathematical operations
Cache-friendly access patterns
Using using statements and Dispose() patterns
Choosing the right allocator for the job
Working with native APIs and raw pointers
Bounds checking and type safety with UnmanagedBuffer<T>
Multiple allocation strategies for different scenarios
graph TD
A[Examples Program] --> B[01-Basic]
A --> C[02-Advanced]
A --> D[03-Allocators]
A --> E[04-Performance]
A --> F[05-RealWorld]
B --> G[Simple Allocation]
C --> H[Defer Patterns]
C --> I[Memory Leak Detection]
D --> J[Hybrid Allocator]
D --> K[System Allocator]
D --> L[Scoped Allocator]
D --> M[Debug Allocator]
D --> N[Memory Pool]
D --> O[Large Block Allocator]
E --> O[Pooling Performance]
E --> P[GC Pressure Avoidance]
F --> Q[Image Processing]
F --> R[Native Interop]
F --> S[Scientific Computing]
- Start with Basic Examples - Understand fundamental concepts
- Explore Allocators - Learn different allocation strategies
- Master Performance Techniques - Optimize your applications
- Apply to Real-World Scenarios - See practical implementations