Bratish Goswami

Personal website and blog

Ran into an interesting issue with Redis today. After running FLUSHDB, the memory usage wasn't going down as expected. The database was empty but Redis was still consuming a lot of memory.

Here's what I learned about Redis memory management:

The Problem:

redis-cli> FLUSHDB
OK
redis-cli> INFO memory
# Memory still showing high usage even though DB is empty

Why this happens:

Redis doesn't immediately return memory to the OS after FLUSHDB. It keeps the allocated memory for future use, which is actually more efficient for performance.

Solutions:

Option 1: Use FLUSHALL instead of FLUSHDB

redis-cli> FLUSHALL

This clears all databases, not just the current one.

Option 2: Force memory cleanup with DEBUG RESTART

redis-cli> DEBUG RESTART

⚠️ Warning: This restarts the Redis server, so use carefully!

Option 3: Let Redis handle it naturally Redis will reuse the allocated memory for new data. The memory will be reclaimed by the OS when Redis actually needs less memory.

Option 4: Check your Redis configuration

redis-cli> CONFIG GET maxmemory
redis-cli> CONFIG GET maxmemory-policy

Option 5: Use SHUTDOWN NOSAVE and restart

redis-cli> SHUTDOWN NOSAVE
# Then restart Redis server

Option 6: Configure Redis to automatically trim memory

redis-cli> CONFIG SET maxmemory-policy allkeys-lru
redis-cli> CONFIG SET maxmemory 100mb

Option 7: Use Redis MEMORY commands (if available)

redis-cli> MEMORY PURGE  # Force garbage collection
redis-cli> MEMORY STATS  # Check memory statistics

Best practice: If you're just testing or cleaning up, FLUSHALL is usually what you want. If you're in production, understand that Redis memory behavior is by design - it's optimized for performance, not for immediately returning memory to the OS.

To monitor memory usage:

redis-cli> INFO memory
redis-cli> MEMORY USAGE keyname

This behavior actually makes sense when you think about it - reallocating memory is expensive, so Redis keeps it around for better performance.