> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MEMORY USAGE

> Estimate memory used by a key.

Use `MEMORY USAGE` to estimate how many bytes a key and its value occupy in memory.

The figure covers the stored data along with its internal overhead, so it is larger than the raw size of the value and is meant for comparing keys rather than for exact accounting. For aggregate types such as hashes, lists, sets, sorted sets, and streams the value is sampled instead of fully traversed: `SAMPLES` sets how many nested elements are inspected, and the value is clamped to the range this deployment supports, so it tunes the estimate rather than forcing an exact traversal. A missing key returns null.

It is the usual way to find out which keys are responsible for memory growth before deciding what to trim or restructure.

## Syntax

```redis theme={"system"}
MEMORY USAGE <key> [SAMPLES <count>]
```

## Arguments

| Argument        | Required | Repeatable | Description                                              |
| --------------- | -------- | ---------- | -------------------------------------------------------- |
| `key`           | Yes      | No         | Key whose in-memory footprint should be estimated.       |
| `SAMPLES count` | No       | No         | Sampling count used when estimating large stream values. |

## Important points

* The result is an estimate in bytes and can change as the internal representation changes.
* A missing key returns null. The sampling count is clamped to the deployment's supported range.

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply                                     |
| -------- | ----------------------------------------- |
| RESP2    | Integer or Null bulk string or null array |
| RESP3    | Integer or Null                           |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>
  <Accordion title="Redis CLI" icon="terminal">
    ```bash theme={"system"}
    MEMORY USAGE my-key SAMPLES 10
    ```
  </Accordion>

  <Accordion title="@upstash/redis" icon="node-js" iconType="brands">
    <Note>
      This command is not supported yet in `@upstash/redis`.
    </Note>
  </Accordion>

  <Accordion title="upstash_redis" icon="python" iconType="brands">
    <Note>
      This command is not supported yet in `upstash_redis`.
    </Note>
  </Accordion>

  <Accordion title="ioredis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import Redis from "ioredis";

    const redis = new Redis(process.env.REDIS_URL!);
    const result = await redis.memory("USAGE", "my-key", "SAMPLES", "10");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="node-redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { createClient } from "redis";

    const client = await createClient({ url: process.env.REDIS_URL })
      .on("error", console.error)
      .connect();
    const result = await client.memoryUsage("my-key", { SAMPLES: 10 });
    console.log(result);
    ```
  </Accordion>

  <Accordion title="redis-py" icon="python" iconType="brands">
    ```python theme={"system"}
    import os
    import redis

    client = redis.from_url(os.environ["REDIS_URL"])
    result = client.memory_usage("my-key", samples=10)
    print(result)
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```go theme={"system"}
    package main

    import (
        "context"
        "fmt"
        "os"

        "github.com/redis/go-redis/v9"
    )

    func main() {
        opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
        if err != nil {
            panic(err)
        }
        client := redis.NewClient(opts)
        result, err := client.MemoryUsage(context.Background(), "my-key", 10).Result()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    }
    ```
  </Accordion>

  <Accordion title="jedis" icon="java" iconType="brands">
    ```java theme={"system"}
    import java.net.URI;

    import redis.clients.jedis.Jedis;

    try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
      Object result = jedis.memoryUsage("my-key", 10);
      System.out.println(result);
    }
    ```
  </Accordion>

  <Accordion title="redis-rs" icon="rust" iconType="brands">
    ```rust theme={"system"}
    fn main() -> redis::RedisResult<()> {
        let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
        let client = redis::Client::open(url)?;
        let mut connection = client.get_connection()?;

        let mut command = redis::cmd("MEMORY");
        command.arg("USAGE");
        command.arg("my-key");
        command.arg("SAMPLES");
        command.arg("10");
        let result: redis::Value = command.query(&mut connection)?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [XACKDEL](/docs/redis/sdks/py/commands/stream/xackdel.md)
- [JSON.DEBUG](/docs/redis/commands/json/json-debug.md)
- [Durable Storage](/docs/redis/features/durability.md)
- [MCP Server](/docs/agent-resources/mcp.md)
