Skip to main content

useMemoryStatus

Description and use case

useMemoryStatus wraps Chromium's non-standard performance.memory API. It polls JavaScript heap telemetry on an interval and flags when used heap crosses a configurable ratio of total allocated heap.

This is useful for heavy dashboards, image editors, virtualized workspaces, and development overlays where memory leaks or garbage collector churn need early visibility.

Browsers that do not expose performance.memory, including Safari and Firefox, return an unsupported state without throwing.

API signature

function useMemoryStatus(options?: UseMemoryStatusOptions): UseMemoryStatusReturn;

Parameters

NameTypeRequiredDescription
options.warningThresholdRationumberNoRatio of used heap to total heap that sets isRiskZone. Default 0.8.
options.intervalnumberNoPolling interval in milliseconds. Default 5000.
options.enabledbooleanNoSet false to disable polling.

Return value

FieldTypeDescription
usedJSHeapSizenumber | nullCurrent used JavaScript heap size in bytes, or null when unsupported.
totalJSHeapSizenumber | nullCurrent total allocated JavaScript heap size in bytes, or null.
jsHeapSizeLimitnumber | nullBrowser JavaScript heap size limit in bytes, or null.
memoryLimitnumber | nullAlias for jsHeapSizeLimit.
isRiskZonebooleanWhether usedJSHeapSize / totalJSHeapSize reached the configured ratio.
isSupportedbooleanWhether this browser exposes performance.memory.

Live interactive demo (StackBlitz)

GitHub Pages does not serve the isolation headers required for embedded StackBlitz WebContainers. Open the demo in StackBlitz to run it interactively.

Code example

import { useMemoryStatus } from 'react-perf-hooks';

export function MemoryStatusBadge() {
const { usedJSHeapSize, totalJSHeapSize, memoryLimit, isRiskZone, isSupported } = useMemoryStatus(
{
warningThresholdRatio: 0.8,
interval: 5000,
},
);

if (!isSupported) {
return <p>Memory telemetry is not available in this browser.</p>;
}

return (
<p>
Heap: {formatBytes(usedJSHeapSize)} / {formatBytes(totalJSHeapSize)} used, limit{' '}
{formatBytes(memoryLimit)}
{isRiskZone ? ' (risk zone)' : ''}
</p>
);
}

function formatBytes(value: number | null) {
if (value === null) return 'n/a';

return `${(value / 1024 / 1024).toFixed(1)} MB`;
}