Файли
openidea/src/features/debugger/memory/MemoryView.tsx
T
l0ng-ai 7dff6e1852 fix(debugger): give Memory class filtering room (#5289)
The Debug Memory toolbar uses the available space for class search and places the Changed only filter in View Options at the right edge. The button shows when the filter is active. The search field shows a clear button when a query is present.
2026-11-23 11:07:14 +08:00

307 рядки
12 KiB
TypeScript

/** Memory tab of the Debug tool window: live instance counts per class of the debugged JVM, and heap capture. */
import { useEffect, useMemo, useRef, useState } from "react";
import clsx from "clsx";
import { Icon, IconButton, List, Menu, SearchField, T } from "@/ui";
import { executeAction } from "@/platform/actions";
import { useTx } from "@/platform/i18n";
import type { JvmClassRow, JvmMemorySnapshot } from "@/platform/ipc";
import { sessionSupports, type DebugSession } from "../state";
import { useActionEnabled } from "../ui/useActionEnabled";
import { retainedForSession, useHeap } from "../heap/heapState";
import {
DEFAULT_QUERY,
isOutdated,
isOvertaken,
refreshMemory,
selectMemoryClass,
setMemoryQuery,
useMemory,
} from "./memoryState";
import { HeapCaptureButton, HeapCaptureStatus } from "./HeapCapture";
import { InstancesPane } from "./InstancesPane";
import { backendText } from "./instancesTree";
import { keepKeysOnButton } from "./rowButtonKeys";
import { columnsOf, formatBytes, formatCount, formatDelta, queryFor, visibleRows, type SortKey } from "./rows";
import { unavailableText } from "./unavailableText";
import styles from "./MemoryView.module.css";
import { useRowHeight } from "@/ui/density";
const COLUMNS: ReadonlyArray<{ key: SortKey; label: string; numeric: boolean }> = [
{ key: "name", label: "Class", numeric: false },
{ key: "instances", label: "Count", numeric: true },
{ key: "deltaInstances", label: "Diff", numeric: true },
{ key: "bytes", label: "Shallow Size", numeric: true },
{ key: "retained", label: "Retained Size", numeric: true },
];
const showInstancesOf = (name: string) => void executeAction("debug.memory.showInstances", name);
/**
* `listsInstances`: the session can list the class's instances, which it reads only while paused.
* `retained`: retained bytes by class name from a snapshot of this table's stop, or `null` for no such column.
*/
function ClassRow({
row,
sizes,
retained,
listsInstances,
paused,
}: {
row: JvmClassRow;
sizes: boolean;
retained: ReadonlyMap<string, number> | null;
listsInstances: boolean;
paused: boolean;
}) {
const tx = useTx();
const delta = formatDelta(row.deltaInstances);
const retainedBytes = retained?.get(row.name);
const opens = listsInstances && paused;
return (
<div className={styles.row} role="none" onDoubleClick={opens ? () => showInstancesOf(row.name) : undefined}>
<span role="gridcell" className={clsx(styles.cell, styles.className)}>
<span className={styles.classText}>{row.name}</span>
{row.ambiguous && (
<span
role="img"
className={styles.ambiguous}
aria-label={tx("Loaded by more than one class loader")}
data-tooltip="Loaded by more than one class loader"
>
<Icon name="warning" size={12} />
</span>
)}
{row.module && <span className={styles.module}>{row.module}</span>}
{listsInstances && (
<IconButton
icon="list"
action="debug.memory.showInstances"
actionArg={row.name}
title="Show Instances"
size="small"
className={styles.rowButton}
disabled={!paused}
onKeyDown={keepKeysOnButton}
/>
)}
</span>
<span role="gridcell" className={clsx(styles.cell, styles.num)}>
{formatCount(row.instances)}
</span>
<span role="gridcell" className={clsx(styles.cell, styles.num, row.deltaInstances === 0 && styles.zero)}>
{delta}
</span>
{sizes && (
<span role="gridcell" className={clsx(styles.cell, styles.num)}>
{formatBytes(row.bytes)}
</span>
)}
{retained && (
<span role="gridcell" className={clsx(styles.cell, styles.num, retainedBytes === undefined && styles.zero)}>
{retainedBytes === undefined ? "—" : formatBytes(retainedBytes)}
</span>
)}
</div>
);
}
export function MemoryView({ session }: { session: DebugSession }) {
const rowHeight = useRowHeight(22);
// Also re-renders the reason text, which reads the catalog when called.
const tx = useTx();
const id = session.id;
const entry = useMemory((s) => s.bySession[id]);
const query = entry?.query ?? DEFAULT_QUERY;
const optionsButton = useRef<HTMLButtonElement>(null);
const [optionsOpen, setOptionsOpen] = useState(false);
const result = entry?.result ?? null;
const snapshot = entry?.snapshot ?? null;
const source = snapshot?.source ?? "histogram";
const heapSnapshots = useHeap((s) => s.snapshots);
const tableStop = snapshot ? (entry?.takenAtStop ?? null) : null;
// `retainedForSession` alone decides whether the sizes fit this table (Z2), so other callers cannot skip a check.
const retained = useMemo(
() => (tableStop === null ? null : retainedForSession(id, tableStop)),
// eslint-disable-next-line react-hooks/exhaustive-deps -- the heap snapshots and the session's stop and status are the inputs of retainedForSession()
[id, tableStop, session.stops, session.status, heapSnapshots],
);
const sorted = useMemo(() => queryFor(query, source, retained !== null), [query, source, retained]);
const listsInstances = sessionSupports("jvmInstances", session);
const paused = session.status === "paused";
const canRefresh = useActionEnabled("debug.memory.refresh");
const backend = backendText(session.jvmBackend, tx);
// Only the first visit loads: later stops mark the table outdated instead of re-running a heap walk.
useEffect(() => {
const e = useMemory.getState().bySession[id];
if (!e?.result && !e?.loading) void refreshMemory(id);
}, [id]);
const overtaken = entry ? isOvertaken(entry, session) : false;
useEffect(() => {
if (overtaken) void refreshMemory(id);
}, [id, overtaken]);
const rows = useMemo(
() => (snapshot ? visibleRows(snapshot.rows, sorted, retained ?? undefined) : []),
[snapshot, sorted, retained],
);
const columns = COLUMNS.filter((c) => columnsOf(source, retained !== null).includes(c.key));
const selected = entry?.selected ?? null;
const sortBy = (key: SortKey) =>
setMemoryQuery(id, sorted.sort === key ? { sort: key, desc: !sorted.desc } : { sort: key, desc: key !== "name" });
return (
<div className={styles.root} aria-busy={entry?.loading || undefined}>
<div className={styles.toolbar} role="toolbar" aria-label={tx("Memory")}>
<IconButton
icon="refresh"
action="debug.memory.refresh"
title="Refresh Memory View"
size="small"
iconSize={16}
disabled={entry?.loading || !canRefresh}
/>
<HeapCaptureButton session={session} />
<SearchField
className={styles.filter}
value={query.filter}
onChange={(filter) => setMemoryQuery(id, { filter })}
placeholder="Filter classes"
aria-label="Filter classes"
/>
<IconButton
ref={optionsButton}
icon="settings"
title="View Options"
size="small"
iconSize={16}
pressed={query.changedOnly}
aria-haspopup="menu"
aria-expanded={optionsOpen}
onClick={() => setOptionsOpen(!optionsOpen)}
/>
<Menu
open={optionsOpen}
onClose={() => setOptionsOpen(false)}
anchor={optionsButton}
placement="bottom-end"
aria-label="View Options"
items={[
{
label: "Changed only",
checked: query.changedOnly,
run: () => setMemoryQuery(id, { changedOnly: !query.changedOnly }),
},
]}
/>
</div>
{backend && (
<div className={styles.banner} role="status">
{backend}
</div>
)}
{entry?.loading && listsInstances && paused && (
<div className={styles.note} role="status">
<T>Counting objects through the debugger. This can take several seconds on a large heap.</T>
</div>
)}
{entry?.error && (
<div className={styles.error} role="alert">
{entry.error}
</div>
)}
<HeapCaptureStatus session={session} />
{snapshot && entry && isOutdated(entry, session.stops) && (
<div className={styles.banner} role="status">
<T>Taken before the last stop. Refresh to update.</T>
</div>
)}
{result?.status === "unavailable" && (
<div className={snapshot ? styles.banner : styles.note} role={snapshot ? "status" : undefined}>
{unavailableText(result.reason)}
{result.canCountWhenPaused && (
<>
{" "}
<span>
<T>Pause the program to count objects through the debugger.</T>
</span>
</>
)}
</div>
)}
{snapshot && (
<div className={clsx(styles.table, source === "debugger" && styles.noSizes, retained && styles.withRetained)}>
<div className={styles.grid} role="grid" aria-label={tx("Memory")} aria-rowcount={rows.length + 1}>
<div className={styles.headGroup} role="rowgroup">
<div className={styles.head} role="row" aria-rowindex={1}>
{columns.map((c) => (
<div
key={c.key}
role="columnheader"
aria-sort={sorted.sort === c.key ? (sorted.desc ? "descending" : "ascending") : "none"}
className={clsx(
styles.headCell,
c.numeric && styles.numeric,
sorted.sort === c.key && styles.sorted,
)}
>
<button type="button" onClick={() => sortBy(c.key)}>
<T>{c.label}</T>
</button>
</div>
))}
</div>
</div>
<List
className={styles.list}
items={rows}
getKey={(r, i) => `${i}:${r.name}`}
renderItem={(r) => (
<ClassRow
row={r}
sizes={source !== "debugger"}
retained={retained}
listsInstances={listsInstances}
paused={paused}
/>
)}
activeIndex={selected == null ? -1 : rows.findIndex((r) => r.name === selected)}
onActiveChange={(i) => selectMemoryClass(id, rows[i]?.name ?? null)}
// A click only selects; Enter opens (double-click is handled by the row).
onActivate={(r, _i, e) => {
if (e.type === "keydown" && listsInstances && paused) showInstancesOf(r.name);
}}
rowHeight={rowHeight}
tableBody={{ headerRows: 1 }}
empty={<span className={styles.note}>{tx("No matches")}</span>}
/>
</div>
<FooterTotals snapshot={snapshot} />
</div>
)}
<InstancesPane session={session} />
</div>
);
}
function FooterTotals({ snapshot }: { snapshot: JvmMemorySnapshot }) {
const tx = useTx();
const n = formatCount(snapshot.rows.length);
const m = formatCount(snapshot.totalInstances);
const size = formatBytes(snapshot.totalBytes);
const counted = snapshot.source === "debugger";
return (
<div className={styles.footer}>
<span>{counted ? tx`${n} classes · ${m} objects` : tx`${n} classes · ${m} objects · ${size}`}</span>
<span className={styles.source}>
{counted ? (
<T>Counted by the debugger: reachable objects only, no sizes.</T>
) : (
<T>Counts include objects that are unreachable but not yet collected.</T>
)}
</span>
</div>
);
}