getting posix_memalign assigning lot of memory in each sesssion of ort session in android, what could be the reason? It appears each model inference is adding same amount of memory of the app usage, how to reduce it?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The issue you're experiencing with posix_memalign allocating excessive memory in ONNX Runtime sessions on Android typically stems from memory management problems. Here are the most common causes and solutions:
Common Causes
1. Session Reuse Issues
Creating new ORT sessions for each inference instead of reusing them
Not properly disposing of previous sessions before creating new ones
2. Memory Arena Configuration
Default memory arena settings may be too aggressive for mobile devices
Pre-allocated memory pools not being reused efficiently
3. Graph Optimization Level
Higher optimization levels can create additional memory allocations for optimized graphs
Solutions
1. Reuse ORT Sessions
cpp
// Create session once and reuse
static std::unique_ptr<Ort::Session> session;
if (!session) {
session = std::make_unique<Ort::Session>(env, model_path, session_options);
}
// Use the same session for multiple inferences
2. Configure Memory Arena
cpp
Ort::SessionOptions session_options;
// Disable memory arena to reduce memory usage
session_options.DisableMemPattern();
session_options.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL);
// Or configure arena with limits
session_options.AddConfigEntry("arena_extend_strategy", "kSameAsRequested");
session_options.AddConfigEntry("memory.enable_memory_arena_shrinkage", "1");
3. Enable Memory Pattern Optimization
cpp
// For mobile scenarios, consider:
session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_BASIC);
session_options.AddConfigEntry("session.use_env_allocators", "1");
4. Properly Release Resources
cpp
// After inference
run_options.reset();
// Clear intermediate tensors
input_tensor.reset();
output_tensors.clear();
// Periodically recreate session if needed
if (inference_count % 100 == 0) {
session.reset();
session = std::make_unique<Ort::Session>(env, model_path, session_options);
}
5. Android-Specific Optimizations
cpp
// Use NNAPI execution provider for better memory management
Ort::SessionOptions session_options;
session_options.AppendExecutionProvider_Nnapi();
// Or limit thread pool size
session_options.SetIntraOpNumThreads(2);
session_options.SetInterOpNumThreads(1);
Use quantized models (INT8) instead of FP32 to reduce memory footprint
Consider using ORT Mobile build which is optimized for mobile devices
Implement a singleton pattern for your inference engine to ensure single session instance
Call System.gc() periodically if using JNI wrapper (though this should be done sparingly)
The key is typically ensuring you're reusing the same session across inferences rather than creating new ones, and configuring the memory arena appropriately for mobile constraints.