MEMORY

Introduction and Use Case(s)

The MEMORY command in Redis is used to inspect various memory-related metrics for a running Redis instance. It helps with understanding memory consumption, identifying memory leaks, and optimizing memory usage. Typical use cases include performance monitoring, debugging applications, and capacity planning.

Syntax

The general syntax of the MEMORY command is as follows:

  1. MEMORY [subcommand] [arguments]

Parameter Explanations

  • subcommand: The specific operation you want to perform. Common subcommands include:

    • USAGE: Reports the memory usage of a key.
    • STATS: Provides general statistics about the memory allocation of the Redis server.
    • PURGE: Clears the internal memory cache (called lazyfree).
    • MALLOC-STATS: Shows detailed low-level memory allocator statistics.
    • HELP: Lists all available subcommands and their descriptions.
  • arguments: Additional parameters required by some subcommands (e.g., key names).

Return Values

The return values vary depending on the subcommand used:

  • USAGE: Returns an integer representing the memory usage in bytes.
  • STATS: Returns a nested array of statistics about memory usage.
  • PURGE: Returns ‘OK’ when the operation is successful.
  • MALLOC-STATS: Returns a bulk string with detailed memory allocator stats.
  • HELP: Returns an array of strings describing each subcommand.

Code Examples

Showing examples using CLI with the dragonfly> prompt:

  1. dragonfly> MEMORY USAGE mykey
  2. (integer) 56
  3. dragonfly> MEMORY STATS
  4. 1) "peak.allocated"
  5. 2) (integer) 8388608
  6. 3) "total.allocated"
  7. 4) (integer) 6250000
  8. 5) "startup.allocated"
  9. 6) (integer) 1000000
  10. 7) "replication.backlog"
  11. 8) (integer) 1048576
  12. 9) "clients.slaves"
  13. 10) (integer) 20480
  14. 11) "clients.normal"
  15. 12) (integer) 102400
  16. 13) "aof.buffer"
  17. 14) (integer) 0
  18. 15) "lua.caches"
  19. 16) (integer) 0
  20. 17) "functions.caches"
  21. 18) (integer) 0
  22. 19) "overhead.total"
  23. 20) (integer) 524288
  24. 21) "keys.count"
  25. 22) (integer) 1000
  26. 23) "keys.bytes-per-key"
  27. 24) (integer) 16
  28. 25) "dataset.bytes"
  29. 26) (integer) 2097152
  30. 27) "dataset.percentage"
  31. 28) "33.33%"
  32. 29) "peak.percentage"
  33. 30) "75.00%"
  34. dragonfly> MEMORY PURGE
  35. "OK"
  36. dragonfly> MEMORY MALLOC-STATS
  37. "___ Begin jemalloc statistics ___\nAllocated: 8388608, active: 12582912, metadata: 4194304...\n___ End jemalloc statistics ___"
  38. dragonfly> MEMORY HELP
  39. 1) "MEMORY DOCTOR"
  40. 2) "MEMORY USAGE <key>"
  41. 3) "MEMORY STATS"
  42. 4) "MEMORY PURGE"
  43. 5) "MEMORY MALLOC-STATS"
  44. 6) "MEMORY HELP"

Best Practices

  • Regularly monitor your memory usage with MEMORY STATS to prevent out-of-memory errors.
  • Use MEMORY PURGE cautiously; it’s helpful for freeing up memory but can impact performance if overused.

Common Mistakes

  • Overlooking memory overhead: Remember that keys, clients, and other structures add significant overhead beyond just the raw data stored.
  • Misinterpreting output: Ensure you understand the units and metrics reported by commands like MEMORY USAGE.

FAQs

What does MEMORY USAGE actually measure?

MEMORY USAGE reports the memory used by the specified key, including associated metadata.

How often should I run MEMORY STATS?

Running MEMORY STATS periodically (e.g., every few minutes) can help you stay informed about your Redis instance’s memory health without causing significant overhead.