Class MemoryMonitor

Class Documentation

class MemoryMonitor

Thread-safe memory usage tracking and limiting system.

The MemoryMonitor class provides centralized memory usage tracking to prevent the application from exceeding system memory limits during intensive processing. It uses atomic operations for thread-safe tracking and provides both current and peak usage statistics.

Features

  • Thread-safe memory usage tracking

  • Configurable memory limits with safety margins

  • Peak usage tracking for performance analysis

  • Integration with system memory detection

  • Automatic memory limit calculation based on thread count

Usage Pattern

  1. Create MemoryMonitor with desired limit

  2. Check can_allocate() before large allocations

  3. Call add_usage() when allocating memory

  4. Call remove_usage() when freeing memory 5. Monitor current and peak usage for optimization

Note

All methods are thread-safe and can be called from multiple threads simultaneously without external synchronization

Public Functions

explicit MemoryMonitor(size_t max_memory_mb = DEFAULT_MEMORY_MB)

Constructor with memory limit specification.

Creates a memory monitor with the specified limit. The limit should account for other system processes and leave sufficient free memory.

Parameters:

max_memory_mb – Maximum memory usage limit in megabytes

bool can_allocate(size_t bytes)

Check if a memory allocation would exceed the limit.

Thread-safe check that should be called before large allocations to prevent memory exhaustion.

Parameters:

bytes – Number of bytes to potentially allocate

Returns:

true if allocation is safe, false if it would exceed limit

void add_usage(size_t bytes)

Record memory allocation in usage tracking.

Updates current usage atomically and tracks peak usage. Should be called immediately after successful allocation.

Parameters:

bytes – Number of bytes allocated

void remove_usage(size_t bytes)

Record memory deallocation in usage tracking.

Updates current usage atomically. Should be called when memory is freed or objects are destroyed.

Parameters:

bytes – Number of bytes freed

size_t get_current_usage() const

Get current memory usage.

Returns:

Current memory usage in bytes

size_t get_peak_usage() const

Get peak memory usage since monitor creation.

Returns:

Peak memory usage in bytes

size_t get_max_usage() const

Get configured memory limit.

Returns:

Maximum allowed memory usage in bytes

void set_memory_limit(size_t max_memory_mb)

Update memory limit during runtime.

Allows dynamic adjustment of memory limits based on system conditions or user preferences.

Parameters:

max_memory_mb – New memory limit in megabytes

Public Static Functions

static size_t get_system_memory_mb()

Detect system memory capacity.

Platform-specific detection of available system memory for automatic limit calculation.

Returns:

Total system memory in megabytes

static size_t calculate_optimal_memory_limit(unsigned int thread_count, size_t system_memory_mb = 0)

Calculate optimal memory limit for given thread count.

Calculates a safe memory limit that accounts for:

  • System memory capacity

  • Number of processing threads

  • Memory overhead for other processes

  • Safety margins for system stability

Parameters:
  • thread_count – Number of threads that will be processing

  • system_memory_mb – Total system memory (0 = auto-detect)

Returns:

Recommended memory limit in megabytes