Posted on

A few hours ago, Salvatore Sanfilippo (the lead developer of Redis), tweeted a little Ruby script to interactively estimate the memory usage of a running redis-server instance:

require 'rubygems'
require 'redis'

def main(opts={})
    r =
    um = 0
    while true do
        newum =[:used_memory]
        if newum != um && um != 0
            diff = newum.to_i-um.to_i
            puts "#{um} bytes (#{diff} difference)"
        um = newum
        sleep 1


I've been experimenting with a Python+Redis combination (with redis-py) for data analysis on a few side projects lately, and a simple script like this can come in handy when you want to make sure you're not doing something completely stupid with Redis that gobbles up all of the allocated memory. And yes, I've been guilty of doing that on a few occasions.

Converting the script from Ruby to Python (with some additional logic for command line option parsing) is very straightforward:

#!/usr/bin/env python

import redis
from optparse import OptionParser
from time import sleep

def options():
    parser = OptionParser()
    parser.add_option("--host", default="localhost")
    parser.add_option("--port", type="int", default=6379)
    return parser.parse_args()

if __name__ == '__main__':
    (opts, args) = options()
    r = redis.Redis(, port=opts.port)
    um = 0

    while True:
        newum =['used_memory']
        if newum != um and um != 0:
            print('%d bytes (%d difference)') % (um, newum - um)
        um = newum