Loading...
Please waitLoading...
Please waitMonitor the memory usage of a web page with useMemory, providing information about the JavaScript heap size.
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-memory
"use client"
import { useMemory } from "@/hooks/use-memory"
export function Component() {
const { isSupported, memory } = useMemory({ interval: 2000, immediate: true })
if (!isSupported) {
return <div>Memory API not supported in this browser.</div>
}
return (
<div>
<h1>Memory Usage</h1>
{memory ? (
<ul>
<li>
JS Heap Size Limit:{" "}
{(memory.jsHeapSizeLimit / 1024 / 1024).toFixed(2)} MB
</li>
<li>
Total JS Heap Size:{" "}
{(memory.totalJSHeapSize / 1024 / 1024).toFixed(2)} MB
</li>
<li>
Used JS Heap Size:{" "}
{(memory.usedJSHeapSize / 1024 / 1024).toFixed(2)} MB
</li>
</ul>
) : (
<p>Loading memory info...</p>
)}
</div>
)
}
Parameter | Type | Description | Default Value | Optional |
---|---|---|---|---|
options | Options | Configuration for memory polling interval and initial fetch. | {} | Yes |
Type | Description |
---|---|
{ isSupported: boolean; memory: MemoryInfo | undefined } | An object containing: - isSupported : Whether performance.memory is supported. - memory : The current memory info, or undefined if not yet fetched or unsupported. |
MemoryInfo
InterfaceProperty | Type | Description |
---|---|---|
jsHeapSizeLimit | number | The maximum size of the JS heap (in bytes). |
totalJSHeapSize | number | The total allocated JS heap size (in bytes). |
usedJSHeapSize | number | The currently used portion of the JS heap (in bytes). |
[Symbol.toStringTag] | "MemoryInfo" | A tag identifying the object as MemoryInfo . |
performance.memory
API is only available in Chromium-based browsers (e.g., Chrome, Edge) and is not supported in Firefox or Safari.isSupported: false
if performance.memory
is unavailable, making it safe for cross-browser use.interval
, and the interval is cleared on component unmount to prevent memory leaks."use client"
for Next.js, ensuring client-side execution.interval
) may slightly impact performance in resource-constrained environments.