Skip to content

Instantly share code, notes, and snippets.

@mathieulongtin
Created October 12, 2016 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathieulongtin/fa2efceb7b546cbb6626ee899e2cfa0b to your computer and use it in GitHub Desktop.
Save mathieulongtin/fa2efceb7b546cbb6626ee899e2cfa0b to your computer and use it in GitHub Desktop.
Prints the number of bytes per db in Redis
import sys
import redis
if len(sys.argv) == 0:
print "Usage: redis-db-mem.py HOST [PORT]"
sys.exit(2)
host = sys.argv[1]
if len(sys.argv) > 2:
port = int(sys.argv[2])
else:
port = 6379
info = redis.StrictRedis(host, port).info()
dbs = [int(_[2:]) for _ in info if _[0:2] == 'db']
dbs.sort()
total_bytes = 0
for db in dbs:
r = redis.StrictRedis(host, port, db=db)
key_count = 0
byte_count = 0
for key in r.scan_iter('*'):
object_dump = r.dump(key)
if object_dump:
key_count += 1
byte_count += len(object_dump) - 8
total_bytes += byte_count
print "db %d: %d objects, %d bytes" % (db, key_count, byte_count)
print "total: %d bytes"
@mathieulongtin
Copy link
Author

This is estimated using the DUMP command. On my servers, I would need to 4X with the result to get the actual memory usage.

@mlissner
Copy link

Well, I expected this to make redis unhappy, but it was enough to take down the server. Not sure which line of code did it, but I'm not ready to figure out. Damn. Looked useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment