Memory
Memory commands for inspecting memory usage, analyzing cache health, and debugging performance issues.
Commands
Section titled âCommandsâ| Command | Syntax | Description |
|---|---|---|
| MEMORY STATS | MEMORY STATS | Get memory statistics |
| MEMORY USAGE | MEMORY USAGE key | Get memory used by a specific key |
| MEMORY DOCTOR | MEMORY DOCTOR | Analyze cache health and get diagnostics |
Examples
Section titled âExamplesâMemory Statistics
Section titled âMemory Statisticsâ127.0.0.1:6379> MEMORY STATS1) "total.allocated"2) (integer) 10485763) "keys.count"4) (integer) 425) "eviction.policy"6) "allkeys-lru"Returns:
total.allocated- Total memory used by all keys (bytes)keys.count- Number of keys in current databaseeviction.policy- Current eviction policy
Memory Usage per Key
Section titled âMemory Usage per Keyâ# Check memory used by a specific key127.0.0.1:6379> SET mykey "Hello World"OK
127.0.0.1:6379> MEMORY USAGE mykey(integer) 56
# Large hash127.0.0.1:6379> HSET user:1 name "Alice" email "alice@example.com" bio "..."(integer) 3
127.0.0.1:6379> MEMORY USAGE user:1(integer) 248
# Non-existent key returns nil127.0.0.1:6379> MEMORY USAGE nonexistent(nil)Memory Doctor
Section titled âMemory DoctorâThe MEMORY DOCTOR command analyzes cache health and provides actionable diagnostics:
127.0.0.1:6379> MEMORY DOCTORSam, I have examined your cache and found:
* Memory: 524288 / 1048576 bytes (50.0% used) - OK: Memory usage is healthy* Eviction policy: allkeys-lru* Disk: 2097152 / 10485760 bytes (20.0% used)* Keys: 150 total, 45 with TTL - OK: No stale expired keys* Key types: - string: 80 - hash: 35 - list: 20 - set: 10 - zset: 5
I'm healthy! No issues found.Doctor Warnings
Section titled âDoctor WarningsâWhen issues are detected:
127.0.0.1:6379> MEMORY DOCTORSam, I have examined your cache and found:
* Memory: 943718 / 1048576 bytes (90.0% used) - WARNING: Memory usage is high (>=90%), eviction is actively occurring* Eviction policy: noeviction - WARNING: noeviction policy with maxmemory set - writes may fail when full* Keys: 1000 total, 200 with TTL - WARNING: 50 expired keys awaiting cleanup (run VACUUM)* Key types: - string: 800 - hash: 150 - list: 50
2 issue(s) detected. Consider addressing the warnings above.Finding Large Keys
Section titled âFinding Large Keysâ# Check memory for each key to find largest127.0.0.1:6379> KEYS *1) "user:1"2) "cache:large"3) "sessions"
127.0.0.1:6379> MEMORY USAGE user:1(integer) 256
127.0.0.1:6379> MEMORY USAGE cache:large(integer) 10485760
127.0.0.1:6379> MEMORY USAGE sessions(integer) 1024Diagnostic Scenarios
Section titled âDiagnostic ScenariosâHigh Memory Usage
Section titled âHigh Memory Usageâ127.0.0.1:6379> MEMORY DOCTOR# Shows: WARNING: Memory usage is high (>=90%)
# Actions:# 1. Check for large keys127.0.0.1:6379> KEYS *# 2. Review eviction policy127.0.0.1:6379> CONFIG GET maxmemory-policy# 3. Increase limit or enable eviction127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lruExpired Keys Not Cleaned
Section titled âExpired Keys Not Cleanedâ127.0.0.1:6379> MEMORY DOCTOR# Shows: WARNING: 50 expired keys awaiting cleanup
# Run vacuum to clean up127.0.0.1:6379> VACUUM(integer) 50 # Keys cleaned
127.0.0.1:6379> MEMORY DOCTOR# Shows: OK: No stale expired keysNo Memory Limit Set
Section titled âNo Memory Limit Setâ127.0.0.1:6379> MEMORY DOCTOR# Shows: INFO: No maxmemory limit set, eviction disabled
# Set a limit to enable eviction127.0.0.1:6379> CONFIG SET maxmemory 104857600OK127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lruOKLibrary Mode (Rust)
Section titled âLibrary Mode (Rust)âuse redlite::Db;
let db = Db::open("mydata.db")?;
// Get total memory usagelet total_bytes = db.total_memory_usage()?;println!("Total memory: {} bytes", total_bytes);
// Get memory for specific keyif let Some(key_id) = db.get_key_id("mykey")? { let bytes = db.calculate_key_memory(key_id)?; println!("Key memory: {} bytes", bytes);}
// Run health checklet report = db.memory_doctor()?;for line in report { println!("{}", line);}
// Configurationdb.set_max_memory(100 * 1024 * 1024); // 100MB limitprintln!("Max memory: {} bytes", db.max_memory());
db.set_eviction_policy(EvictionPolicy::AllKeysLru);println!("Policy: {}", db.eviction_policy().to_str());Understanding Memory Usage
Section titled âUnderstanding Memory UsageâWhatâs Counted
Section titled âWhatâs CountedâMemory usage includes:
- Key metadata (name, type, TTL, timestamps)
- String values (raw bytes)
- Hash field-value pairs
- List elements
- Set members
- Sorted set members with scores
- Stream entries
- JSON documents
Memory vs Disk
Section titled âMemory vs DiskâMEMORY USAGEshows logical data size- Actual SQLite file may be larger due to:
- Page overhead
- Free space from deletions
- WAL file (if using WAL mode)
Use VACUUM to reclaim disk space after deleting many keys.
Use Cases
Section titled âUse CasesâCapacity Planning
Section titled âCapacity Planningâ# Monitor memory growthMEMORY STATS# ... add data ...MEMORY STATS
# Set appropriate limitsCONFIG SET maxmemory 1073741824 # 1GBCONFIG SET maxmemory-policy allkeys-lruDebugging Memory Issues
Section titled âDebugging Memory Issuesâ# Regular health checksMEMORY DOCTOR
# Find memory hogsSCAN 0 COUNT 100# For each key:MEMORY USAGE <key>Cache Optimization
Section titled âCache Optimizationâ# Check if eviction is workingMEMORY DOCTOR# Look for: "eviction is actively occurring"
# Verify expired key cleanupMEMORY DOCTOR# Look for: "expired keys awaiting cleanup"VACUUM