Overview
Off Grid uses aggressive memory management to prevent crashes and ensure smooth operation on mobile devices with limited RAM. The system calculates memory budgets, estimates model memory usage, and enforces runtime safeguards before loading models.Memory Budget Calculation
Off Grid uses a 60% safe limit of total device RAM as the maximum budget for AI models.Budget Formula
Warning Thresholds
The 60% limit accounts for OS overhead, background processes, and React Native’s JavaScript runtime. Exceeding this limit risks system memory pressure and app termination.
Example Budget by Device
Model Memory Estimates
Before loading a model, the system estimates total RAM usage based on model type and file size.Text Models
Formula:- Model weights loaded into memory
- KV (key-value) cache for context window
- Activations during inference
- llama.cpp runtime buffers
- Qwen3 0.6B Q4_K_M: 395 MB × 1.5 = ~593 MB
- Llama 3.2 3B Q4_K_M: 2.0 GB × 1.5 = ~3.0 GB
- Qwen3 7B Q4_K_M: 4.0 GB × 1.5 = ~6.0 GB
Vision Models
Formula:- Main GGUF model file
- mmproj (multimodal projector) companion file
- Combined size × 1.5 for overhead
-
SmolVLM 500M:
- Model: 475 MB
- mmproj: 125 MB
- Total: (475 + 125) × 1.5 = ~900 MB
-
Qwen3-VL 2B:
- Model: 1.2 GB
- mmproj: 350 MB
- Total: (1.2 + 0.35) × 1.5 = ~2.3 GB
Image Generation Models
Formula:- Model weights (UNet, VAE, text encoder)
- MNN/QNN runtime allocations
- Intermediate tensors during denoising
- Image preview buffers
- SD 1.5 Palettized (iOS): 1.0 GB × 1.8 = ~1.8 GB
- Anything V5 MNN (Android): 1.2 GB × 1.8 = ~2.2 GB
- SD 1.5 Full (iOS): 4.0 GB × 1.8 = ~7.2 GB
Pre-Load Memory Checks
Before loading any model, the system performs RAM validation.Check Process
Memory Check Results
Pre-load checks prevent out-of-memory crashes by blocking loads that would exceed the 60% RAM budget. Users must unload the current model or choose a smaller model.
RAM-Aware Runtime Safeguards
On low-RAM devices, llama.cpp and CLIP can callabort() during Metal/OpenCL GPU allocation, bypassing JavaScript error handling and crashing the app instantly. To prevent this, the system applies device-RAM-based caps before any native call.
Device-Specific Caps
Implementation
GPU Layer Override:Why These Safeguards Matter
Without safeguards:- iPhone XS (4GB RAM) attempts Metal GPU allocation
- Metal buffer allocation fails, calls
abort() - POSIX signal bypasses JavaScript try/catch
- App terminates immediately with SIGABRT
- System detects 4GB RAM before native call
- GPU layers forced to 0 (CPU-only mode)
- Context length capped at 2048
- CLIP GPU disabled
- App runs stably without crashes
Memory Monitoring
The hardware service provides real-time memory tracking.Get Current Memory Usage
Model Memory Helpers
Memory monitoring uses
react-native-device-info for system-level memory stats. Native allocations (llama.cpp, MNN/QNN) may not be fully reflected immediately in JavaScript layer.User-Friendly Error Messages
When memory checks fail, the system provides actionable error messages.Example Messages
Critical (blocked load):Best Practices
1. Choose Models Based on Device RAM
- 4GB RAM: Qwen3 0.6B, SmolLM3 135M (Q2_K, Q3_K_M)
- 6GB RAM: Llama 3.2 3B, Qwen3 1.6B (Q4_K_M)
- 8GB RAM: Qwen3 7B, Llama 3.2 3B (Q4_K_M, Q5_K_M)
- 12GB+ RAM: Qwen3 14B, Llama 3.3 8B (Q5_K_M, Q6_K, Q8_0)
2. Unload Models When Switching
Always unload the current model before loading a new one to free RAM:3. Monitor Memory During Development
Use the DeviceInfoScreen to check real-time memory usage:- Total RAM
- Used RAM
- Available RAM
- Current model memory estimate
4. Test on Low-RAM Devices
Always test new features on devices with 4-6GB RAM to ensure safeguards work correctly.5. Respect Warning Thresholds
If a model triggers a 50-60% warning:- Close background apps
- Reduce context length
- Consider smaller quantization
- Monitor for performance degradation
Memory Overhead Breakdown
Text Model Overhead (1.5x multiplier)
Image Model Overhead (1.8x multiplier)
Vision Model Overhead (1.5x multiplier)
Troubleshooting Memory Issues
App Crashes on Model Load
Symptoms: App terminates immediately when loading a model Causes:- Model exceeds device RAM budget
- GPU allocation failure on low-RAM device
- Corrupted model file
- Check RAM budget:
deviceRAM × 0.60 > estimatedModelRAM - Disable GPU offloading (set layers to 0)
- Re-download model file
- Choose smaller model or lower quantization
Slow Performance After Loading Model
Symptoms: App becomes sluggish, inference is very slow Causes:- Model using 50-60% of RAM (memory pressure)
- System swapping memory to disk
- Thermal throttling
- Close background apps
- Reduce context length (2048 → 512)
- Choose smaller model
- Wait for device to cool down
”Cannot Load Model” Error
Symptoms: Red error message, load blocked Causes:- Estimated RAM usage exceeds 60% budget
- Another model already loaded
- Unload current model first
- Choose smaller model or lower quantization
- Close other apps to free system RAM
Vision Model Missing mmproj
Symptoms: Vision model fails to load, mmproj not found Causes:- mmproj file not downloaded
- mmproj file in wrong directory
- System automatically searches model directory for mmproj
- Re-download model (automatic mmproj download)
- Manually place mmproj file in same directory as model GGUF