# 241: MEMORY_LIMIT_EXCEEDED (code 241)

> The query would exceed the memory limit set by max_memory_usage or max_server_memory_usage.

Source: https://sqlly.app/errors/clickhouse/memory-limit-exceeded
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Error code: `241`
- Name: `MEMORY_LIMIT_EXCEEDED`
- Category: Resource limits

Error message:

```text
Memory limit (for query) exceeded: would use {bytes}
```

## What it means

`MEMORY_LIMIT_EXCEEDED` (241) fires when a query would push past `max_memory_usage` per query or the server total limit. The message shows the bytes it would have used. GROUP BY over wide data, ORDER BY without limits, and huge joins are the usual offenders.

## Common causes

- A large GROUP BY or DISTINCT that does not fit the per-query limit.
- ORDER BY over a big result without `max_bytes_before_external_sort` configured.
- Many heavy queries at once exhausting the server total, not one query alone.

## How to fix it

1. Let GROUP BY spill to disk instead of aborting.

   ```sql
   SET max_bytes_before_external_group_by = 20000000000; SET max_memory_usage = 40000000000;
   ```
2. Sort externally the same way.

   ```sql
   SET max_bytes_before_external_sort = 20000000000, max_bytes_ratio_before_external_sort = 0;
   ```
3. Cap the result with LIMIT so ORDER BY keeps only the top rows in memory.

   ```sql
   SELECT * FROM big ORDER BY ts DESC LIMIT 100;
   ```
4. Raise the per-query limit for a heavy session without touching the server default.

   ```sql
   SET max_memory_usage = 50000000000;
   ```

## Reproduce it safely

groupArray materializes every value; the final array is about 80 MB for 10 million numbers and the query peaks near 257 MiB, so the 10 MB limit fires deterministically; a count() grouped by number % 1000 holds only a few thousand tiny states and never gets close.

```sql
SELECT groupArray(number) FROM numbers(10000000) SETTINGS max_memory_usage = 10000000; raises code 241.
```

## How SQLly helps

SQLly's activity monitor lists running queries with elapsed time and rows, and you can kill the heavy one without opening clickhouse-client.

## Questions

### Should I just raise max_memory_usage?

Only if the server has the RAM. Prefer spilling to disk and LIMIT-first patterns; raising the limit moves the failure to the OS OOM killer.

### Where do I see which query used the most memory?

SELECT query_id, memory_usage, query FROM system.query_log ORDER BY memory_usage DESC.

## Related errors

- [`202`](https://sqlly.app/errors/clickhouse/too-many-simultaneous-queries): TOO_MANY_SIMULTANEOUS_QUERIES (code 202)
- [`159`](https://sqlly.app/errors/clickhouse/timeout-exceeded): TIMEOUT_EXCEEDED (code 159)

The code and message text are from the [ClickHouse documentation (system.errors)](https://clickhouse.com/docs/en/operations/system-tables/errors).
