From 57807cd33806116d2d65c2e037ffa35cdadfd801 Mon Sep 17 00:00:00 2001 From: Ofir Luzon Date: Thu, 13 Feb 2025 17:18:47 -0800 Subject: [PATCH] Memory Usage command LIST accuracy fix (#13783) MEMORY USAGE on a List samples quicklist entries, but does not account to how many elements are in each sampled node. This can skew the calculation when the sampled nodes are not balanced. The fix calculate the average element size in the sampled nodes instead of the average node size. --- src/object.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/object.c b/src/object.c index 6a03f26a7..45acd2e83 100644 --- a/src/object.c +++ b/src/object.c @@ -988,7 +988,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { dict *d; dictIterator *di; struct dictEntry *de; - size_t asize = 0, elesize = 0, samples = 0; + size_t asize = 0, elesize = 0, elecount = 0, samples = 0; if (o->type == OBJ_STRING) { if(o->encoding == OBJ_ENCODING_INT) { @@ -1007,9 +1007,10 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) { asize = sizeof(*o)+sizeof(quicklist); do { elesize += sizeof(quicklistNode)+zmalloc_size(node->entry); + elecount += node->count; samples++; } while ((node = node->next) && samples < sample_size); - asize += (double)elesize/samples*ql->len; + asize += (double)elesize/elecount*ql->count; } else if (o->encoding == OBJ_ENCODING_LISTPACK) { asize = sizeof(*o)+zmalloc_size(o->ptr); } else {