获取内存中的redis键值大小

时间:2018-03-20 15:45:14

标签: memory redis redis-cli

我试图获取键值或只是一个键的大小或只是redis中的值。

使用debug object key命令返回键值的序列化大小(如果要写入磁盘),而不是它在内存中使用的实际字节数。

这可以通过检查源代码(基于此Redis: Show database size/size for keys

来确认

https://github.com/antirez/redis/blob/4082c38a60eedd524c78ef48c1b241105f4ddc50/src/debug.c#L337-L343

https://github.com/antirez/redis/blob/4082c38a60eedd524c78ef48c1b241105f4ddc50/src/rdb.c#L663-L671

查看源代码:

/* Save a string object as [len][data] on disk. If the object is a string
 * representation of an integer value we try to save it in a special form */
ssize_t rdbSaveRawString(rio *rdb, unsigned char *s, size_t len) {
  int enclen;
  ssize_t n, nwritten = 0;

  /* Try integer encoding */
  if (len <= 11) {
    unsigned char buf[5];
    if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) {
      if (rdbWriteRaw(rdb,buf,enclen) == -1) return -1;
      return enclen;
    }
  }

  /* Try LZF compression - under 20 bytes it's unable to compress even
   * aaaaaaaaaaaaaaaaaa so skip it */
  if (server.rdb_compression && len > 20) {
    n = rdbSaveLzfStringObject(rdb,s,len);
    if (n == -1) return -1;
    if (n > 0) return n;
    /* Return value of 0 means data can't be compressed, save the old way */
  }

  /* Store verbatim */
  if ((n = rdbSaveLen(rdb,len)) == -1) return -1;
  nwritten += n;
  if (len > 0) {
    if (rdbWriteRaw(rdb,s,len) == -1) return -1;
    nwritten += len;
  }
  return nwritten;
}

通过redis-cli确认:

127.0.0.1:6379> set a aaaaaaaaaaaaaaaaaaa
OK
127.0.0.1:6379> debug object a
Value at:0x7f985822f168 refcount:1 encoding:embstr serializedlength:20 lru:11611136 lru_seconds_idle:2
127.0.0.1:6379> set a aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OK
127.0.0.1:6379> debug object a
Value at:0x7f985827c428 refcount:1 encoding:embstr serializedlength:12 lru:11611147 lru_seconds_idle:1

所有不同的CLI工具都报告了对象的序列化大小,而不是内存大小,这是有趣且重要的。

1 个答案:

答案 0 :(得分:1)

从Redis v4开始,MEMORY USAGE命令在猜测密钥的足迹及其值方面做得更好。