Файли
openidea/src/features/debugger/heap/editor/heapQuery.ts
T
ntbang0901 8d09479a65 feat(debugger): heap snapshots of a debugged JVM (#2649)
Capture a heap dump of the debugged JVM over the attach listener (GC.heap_dump, after checking free disk space and the JDK's rules for suspended VMs) or open an .hprof file, and browse it in an editor tab: classes with shallow and retained sizes, biggest objects, dominators, the path from an object to a GC root, and a comparison of two snapshots. A snapshot captured at a stop adds a Retained Size column to that stop's Memory tab.

New crate ide-heap: HotSpot hprof parser (bounds-checked; unfinished or crafted dumps are refused), object graph, dominator tree (semi-NCA), retained sizes and GC-root paths. Snapshots live in private directories removed with their window; an opened dump is analyzed from a private copy so changes to the user's file can't affect the mapped data.

Co-authored-by: ntbang0901 <[email protected]>
2026-11-11 17:43:07 +08:00

40 рядки
1.3 KiB
TypeScript

import { useEffect, useEffectEvent, useState } from "react";
import { tx } from "@/platform/i18n";
import { IpcError } from "@/platform/ipc";
/** The host answers `notFound` for a snapshot that another window opened (or that its window already closed). */
export function heapErrorText(e: unknown): string {
if (e instanceof IpcError && e.kind === "notFound") return tx("This heap snapshot is not available in this window.");
return e instanceof Error ? e.message : String(e);
}
export interface HeapQuery<T> {
data: T | undefined;
error: string | null;
}
/**
* Runs `load` whenever `key` changes (nothing while it is `null`) and keeps the latest answer; an answer for an earlier
* key is dropped.
*/
export function useHeapQuery<T>(key: string | null, load: () => Promise<T>): HeapQuery<T> {
const [state, setState] = useState<HeapQuery<T> & { key: string | null }>({
key: null,
data: undefined,
error: null,
});
const run = useEffectEvent(load);
useEffect(() => {
if (key === null) return;
let live = true;
run().then(
(data) => live && setState({ key, data, error: null }),
(e: unknown) => live && setState({ key, data: undefined, error: heapErrorText(e) }),
);
return () => {
live = false;
};
}, [key]);
return state.key === key ? state : { data: undefined, error: null };
}