title_wip

[Edit this page](https://github.com/ClickHouse/clickhouse-docs/blob/main/docs/guides/developer/debugging-memory-issues.md)

When encountering memory issues or a memory leak, knowing what queries and resources are consuming a significant amount of memory is helpful. Below you can find queries that can help you to debug memory issues by finding which queries, databases, and tables can be optimized:

## List currently running processes by peak memory usage[​](#list-currently-running-processes-by-peak-memory "Direct link to List currently running processes by peak memory usage")

```
SELECT
    initial_query_id,
    query,
    elapsed,
    formatReadableSize(memory_usage),
    formatReadableSize(peak_memory_usage),
FROM system.processes
ORDER BY peak_memory_usage DESC
LIMIT 100;
```

## List metrics for memory usage[​](#list-metrics-for-memory-usage "Direct link to List metrics for memory usage")

```
SELECT
    metric, description, formatReadableSize(value) size
FROM
    system.asynchronous_metrics
WHERE
    metric LIKE '%Cach%'
    OR metric LIKE '%Mem%'
ORDER BY
    value DESC;
```

## List tables by current memory usage[​](#list-tables-by-current-memory-usage "Direct link to List tables by current memory usage")

```
SELECT
    database,
    name,
    formatReadableSize(total_bytes)
FROM system.tables
WHERE engine IN ('Memory','Set','Join');
```

## Output total memory used by merges[​](#output-total-memory-used-by-merges "Direct link to Output total memory used by merges")

```
SELECT formatReadableSize(sum(memory_usage)) FROM system.merges;
```

## Output total memory used by currently running processes[​](#output-total-memory-used-by-currently-running-processes "Direct link to Output total memory used by currently running processes")

```
SELECT formatReadableSize(sum(memory_usage)) FROM system.processes;
```

## Output total memory used by dictionaries[​](#output-total-memory-used-by-dictionaries "Direct link to Output total memory used by dictionaries")

```
SELECT formatReadableSize(sum(bytes_allocated)) FROM system.dictionaries;
```

## Output total memory used by primary keys and index granularity[​](#output-total-memory-used-by-primary-keys "Direct link to Output total memory used by primary keys and index granularity")

```
SELECT
    sumIf(data_uncompressed_bytes, part_type = 'InMemory') AS memory_parts,
    formatReadableSize(sum(primary_key_bytes_in_memory)) AS primary_key_bytes_in_memory,
    formatReadableSize(sum(primary_key_bytes_in_memory_allocated)) AS primary_key_bytes_in_memory_allocated,
    formatReadableSize(sum(index_granularity_bytes_in_memory)) AS index_granularity_bytes_in_memory,
    formatReadableSize(sum(index_granularity_bytes_in_memory_allocated)) AS index_granularity_bytes_in_memory_allocated
FROM system.parts;
```

- [List currently running processes by peak memory usage](#list-currently-running-processes-by-peak-memory)- [List metrics for memory usage](#list-metrics-for-memory-usage)- [List tables by current memory usage](#list-tables-by-current-memory-usage)- [Output total memory used by merges](#output-total-memory-used-by-merges)- [Output total memory used by currently running processes](#output-total-memory-used-by-currently-running-processes)- [Output total memory used by dictionaries](#output-total-memory-used-by-dictionaries)- [Output total memory used by primary keys and index granularity](#output-total-memory-used-by-primary-keys)

Was this page helpful?