Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setting bit 128,000,000 in a Redis bitmap allocates ~16MB of RAM
# 1. Check the RSS of the running Redis instance.
# 2. Set the 128 millionth bit in a new bitmap keyed "test0"
# 3. Check the RSS again to see how much it's increased.
#
# Note that Redis will allocate as much memory as it needs to set
# the given bit. Even though we don't set any bit prior to the 128
# millionth, it will allocate ~16MB RAM to set that bit.
$ ps -axm -o rss,comm | grep redis-server
6800 redis-server *:6379
$ redis-cli
127.0.0.1:6379> SETBIT test0 128000000 1
(integer) 0
$ ps -axm -o rss,comm | grep redis-server
22768 redis-server *:6379
# Setting the 128 millionth bit allocated roughly 16MB RAM:
#
# 22768 KB - 6800 KB = 15968 KB ~= 16 MB
#
# You can also use STRLEN and DEBUG SDSLEN to see this behavior in action via the Redis CLI:
$ redis-cli
127.0.0.1:6379> SETBIT test0 128000000 1
(integer) 0
key_sds_len:5, key_sds_avail:0, key_zmalloc: 16, val_sds_len:16000001, val_sds_avail:7157, val_zmalloc: 16007168
127.0.0.1:6379> STRLEN test0
(integer) 16000001
127.0.0.1:6379> DEBUG SDSLEN test0
key_sds_len:5, key_sds_avail:0, key_zmalloc: 16, val_sds_len:16000001, val_sds_avail:7157, val_zmalloc: 16007168
@voberoi
Copy link
Author

voberoi commented Aug 10, 2022

Thanks @pjambet for the SDSLEN tip!

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