Skip to content

Fix MEMORY USAGE command#5

Open
tomerqodo wants to merge 2 commits intocursor_full_base_fix_memory_usage_command_pr5from
cursor_full_head_fix_memory_usage_command_pr5
Open

Fix MEMORY USAGE command#5
tomerqodo wants to merge 2 commits intocursor_full_base_fix_memory_usage_command_pr5from
cursor_full_head_fix_memory_usage_command_pr5

Conversation

@tomerqodo
Copy link
Copy Markdown

@tomerqodo tomerqodo commented Jan 26, 2026

Benchmark PR from agentic-review-benchmarks#5


Note

Improves accuracy of memory introspection and aligns it with kv-embedded objects.

  • Replace objectComputeSize with kvobjComputeSize that includes kvobj header and embedded key via malloc_usable_size, and refine per-type size accounting (strings, lists/quicklist sampling, sets, zsets, hashes, streams, modules)
  • Use kvobjComputeSize in MEMORY USAGE command; update comments and help text accordingly
  • Fix sampling math for quicklist average and stream listpack aggregation
  • Add/adjust tests in string.tcl, list.tcl, and hash.tcl to assert nonzero/expected MEMORY USAGE, including string size bounds and jemalloc-specific cases

Written by Cursor Bugbot for commit b6ee4f1. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 3 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread src/object.c
samples++;
} while ((node = node->next) && samples < sample_size);
asize += (double)elesize/elecount*ql->count;
asize += (double)elesize/samples*ql->count;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quicklist divides by nodes instead of items

High Severity

The memory calculation for quicklist divides by samples (number of nodes) instead of elecount (number of items). Since quicklist nodes contain multiple items, this causes a unit mismatch: elesize/samples gives bytes-per-node, but it's then multiplied by ql->count (total items). This will massively over-report memory usage—by roughly a factor equal to the average items per node.

Fix in Cursor Fix in Web

Comment thread src/object.c
} else {
if (samples) lpsize /= samples; /* Compute the average. */
asize += lpsize * (s->rax->numele-1);
asize += lpsize * s->rax->numele;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream double-counts last listpack in memory estimate

Medium Severity

The change from lpsize * (s->rax->numele - 1) to lpsize * s->rax->numele contradicts the documented algorithm in the comment above. The code computes the average size for ALL listpacks, then explicitly adds the last listpack's actual size on line 1336, causing double-counting of the last listpack.

Fix in Cursor Fix in Web

Comment thread src/object.c
size_t elesize = 0, elecount = 0, samples = 0;

/* All kv-objects has at least kvobj header and embedded key */
size_t asize = malloc_usable_size((void *)o);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses wrong allocator function causing portability issues

High Severity

The code uses malloc_usable_size() directly instead of the Redis wrapper zmalloc_size(). When Redis is built with jemalloc (the default for production), this calls glibc's function on jemalloc-allocated memory, returning incorrect sizes. On macOS, malloc_usable_size doesn't exist at all (macOS uses malloc_size), causing compilation failure. The rest of this same function correctly uses zmalloc_size() for all other allocation size queries.

Fix in Cursor Fix in Web

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants