Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | /**
* Memory information at a point in time
*/
export interface MemoryInfo {
/** Used JS heap size in bytes */
heapUsed: number;
/** Total JS heap size in bytes */
heapTotal: number;
/** JS heap size limit in bytes */
heapLimit: number;
/** Timestamp when this measurement was taken */
timestamp: number;
}
/**
* A named memory snapshot for comparison
*/
export interface MemorySnapshot {
/** Unique identifier for this snapshot */
id: string;
/** Memory information at snapshot time */
memory: MemoryInfo;
/** Number of DOM nodes (if tracking enabled) */
domNodes?: number;
/** Estimated event listener count (if tracking enabled) */
eventListeners?: number;
/** Timestamp when snapshot was taken */
timestamp: number;
}
/**
* Result of comparing two memory snapshots
*/
export interface SnapshotDiff {
/** Difference in heap usage (bytes) */
heapDelta: number;
/** Percentage change in heap usage */
heapPercentChange: number;
/** Difference in DOM node count */
domNodesDelta?: number;
/** Difference in event listener count */
eventListenersDelta?: number;
/** Time elapsed between snapshots (ms) */
timeDelta: number;
}
/**
* GC (Garbage Collection) event detected in memory samples
*/
export interface GCEvent {
/** Index in the sample array where GC was detected */
sampleIndex: number;
/** Memory before GC (bytes) */
memoryBefore: number;
/** Memory after GC (bytes) */
memoryAfter: number;
/** Recovery amount (bytes freed) */
recoveryAmount: number;
/** Recovery ratio (0-1) */
recoveryRatio: number;
/** Timestamp of GC event */
timestamp: number;
}
/**
* GC analysis result
*/
export interface GCAnalysis {
/** Number of GC events detected */
gcEventCount: number;
/** All detected GC events */
gcEvents: GCEvent[];
/** Average memory recovery ratio after GC (0-1) */
avgRecoveryRatio: number;
/** Timestamp of last GC event */
lastGCTimestamp: number | null;
/** Whether GC is effectively reclaiming memory */
isGCEffective: boolean;
}
/**
* Baseline analysis for leak detection
*/
export interface BaselineAnalysis {
/** Baseline heap value (average of post-GC minimums) */
baselineHeap: number;
/** Current heap value */
currentHeap: number;
/** Growth from baseline (bytes) */
growthFromBaseline: number;
/** Growth ratio from baseline (0-1) */
growthRatio: number;
/** Whether baseline is established (enough GC cycles observed) */
isBaselineEstablished: boolean;
/** Whether growth from baseline exceeds threshold */
isSignificantGrowth: boolean;
}
/**
* Result of memory leak analysis
*/
export interface LeakAnalysis {
/** Whether a memory leak is detected */
isLeaking: boolean;
/** Probability of memory leak (0-100) */
probability: number;
/** Memory usage trend */
trend: Trend;
/** Average memory growth per interval (bytes) */
averageGrowth: number;
/** R-squared value indicating regression fit quality (0-1) */
rSquared: number;
/** Samples used for analysis */
samples: MemoryInfo[];
/** Human-readable recommendation */
recommendation?: string;
/** GC analysis results */
gcAnalysis?: GCAnalysis;
/** Baseline analysis results */
baselineAnalysis?: BaselineAnalysis;
/** Total observation time in milliseconds */
observationTime?: number;
/** Confidence level of the analysis (0-100) */
confidence?: number;
/** Reasons contributing to the probability score */
factors?: LeakProbabilityFactors;
}
/**
* Factors contributing to leak probability calculation
*/
export interface LeakProbabilityFactors {
/** Contribution from slope analysis (0-30) */
slopeContribution: number;
/** Contribution from R² fit quality (0-20) */
rSquaredContribution: number;
/** Contribution from GC ineffectiveness (0-25) */
gcContribution: number;
/** Contribution from observation time (0-15) */
timeContribution: number;
/** Contribution from baseline growth (0-10) */
baselineContribution: number;
}
/**
* Information about why memory monitoring is not supported
*/
export interface UnsupportedInfo {
/** Reason for lack of support */
reason: UnsupportedReason;
/** Browser name if detected */
browser?: string;
/** Available fallback strategies */
availableFallbacks: FallbackStrategy[];
}
/**
* Reason why memory API is not supported
*/
export type UnsupportedReason =
| "no-api"
| "server-side"
| "insecure-context"
| "browser-restriction";
/**
* Level of memory API support
*/
export type SupportLevel = "full" | "partial" | "none";
/**
* Available memory metrics based on browser support
*/
export type AvailableMetric =
| "heapUsed"
| "heapTotal"
| "heapLimit"
| "domNodes"
| "eventListeners";
/**
* Memory usage severity level
*/
export type Severity = "normal" | "warning" | "critical";
/**
* Memory usage trend direction
*/
export type Trend = "stable" | "increasing" | "decreasing";
/**
* Fallback strategy for unsupported browsers
*/
export type FallbackStrategy = "none" | "estimation" | "dom-only";
/**
* Leak detection sensitivity level
*/
export type LeakSensitivity = "low" | "medium" | "high";
/**
* Warning data passed to onWarning callback
*/
export interface MemoryWarning {
/** Current memory info */
memory: MemoryInfo;
/** Current usage percentage */
usagePercentage: number;
/** Warning threshold that was exceeded */
threshold: number;
/** Timestamp of warning */
timestamp: number;
}
/**
* Critical alert data passed to onCritical callback
*/
export interface MemoryCritical {
/** Current memory info */
memory: MemoryInfo;
/** Current usage percentage */
usagePercentage: number;
/** Critical threshold that was exceeded */
threshold: number;
/** Timestamp of critical alert */
timestamp: number;
}
/**
* Browser support detection result
*/
export interface BrowserSupport {
/** Level of support */
level: SupportLevel;
/** Available metrics in this browser */
availableMetrics: AvailableMetric[];
/** Any limitations or caveats */
limitations: string[];
/** Whether secure context requirements are met */
isSecureContext: boolean;
/** Whether cross-origin isolated */
isCrossOriginIsolated: boolean;
/** Whether precise memory API is available */
hasPreciseMemoryAPI: boolean;
}
/**
* Formatted memory values for display
*/
export interface FormattedMemory {
/** Formatted heap used (e.g., "45.2 MB") */
heapUsed: string;
/** Formatted heap total (e.g., "100 MB") */
heapTotal: string;
/** Formatted heap limit (e.g., "2 GB") */
heapLimit: string;
/** Formatted DOM node count */
domNodes?: string;
/** Formatted event listener count */
eventListeners?: string;
}
/**
* Leak detection configuration
*/
export interface LeakDetectionOptions {
/** Enable leak detection */
enabled?: boolean;
/** Detection sensitivity */
sensitivity?: LeakSensitivity;
/** Number of samples to analyze */
windowSize?: number;
/** Custom growth rate threshold (bytes/sample) */
threshold?: number;
}
/**
* Threshold configuration for alerts
*/
export interface ThresholdOptions {
/** Warning threshold percentage (0-100) */
warning?: number;
/** Critical threshold percentage (0-100) */
critical?: number;
}
/**
* Configuration options for useMemoryMonitor hook
*/
export interface UseMemoryMonitorOptions {
// Basic Settings
/** Monitoring interval in milliseconds (default: 5000) */
interval?: number;
/** Auto-start monitoring on mount (default: true) */
autoStart?: boolean;
/** Enable monitoring (default: true) */
enabled?: boolean;
// History
/** Enable history recording (default: false) */
enableHistory?: boolean;
/** Maximum history size (default: 50) */
historySize?: number;
// Thresholds
/** Threshold configuration for warnings/alerts */
thresholds?: ThresholdOptions;
// Leak Detection
/** Leak detection configuration */
leakDetection?: LeakDetectionOptions;
// Development Features
/** Enable development mode features (default: false) */
devMode?: boolean;
/** Track DOM node count (default: false) */
trackDOMNodes?: boolean;
/** Track event listener count (default: false) */
trackEventListeners?: boolean;
/** Log updates to console (default: false) */
logToConsole?: boolean;
// Callbacks
/** Called on each memory update */
onUpdate?: (memory: MemoryInfo) => void;
/** Called when warning threshold is exceeded */
onWarning?: (data: MemoryWarning) => void;
/** Called when critical threshold is exceeded */
onCritical?: (data: MemoryCritical) => void;
/** Called when memory leak is detected */
onLeakDetected?: (analysis: LeakAnalysis) => void;
/** Called when monitoring is not supported */
onUnsupported?: (info: UnsupportedInfo) => void;
// Advanced Settings
/** Disable in production builds (default: false) */
disableInProduction?: boolean;
/** Fallback strategy for unsupported browsers (default: 'dom-only') */
fallbackStrategy?: FallbackStrategy;
}
/**
* Return type for useMemoryMonitor hook
*/
export interface UseMemoryMonitorReturn {
// Current State
/** Current memory information (null if unsupported) */
memory: MemoryInfo | null;
/** Used JS heap in bytes (null if unsupported) */
heapUsed: number | null;
/** Total JS heap in bytes (null if unsupported) */
heapTotal: number | null;
/** JS heap limit in bytes (null if unsupported) */
heapLimit: number | null;
/** Memory usage percentage (null if unsupported) */
usagePercentage: number | null;
// DOM Related
/** Current DOM node count (null if not tracking) */
domNodes: number | null;
/** Estimated event listener count (null if not tracking) */
eventListeners: number | null;
// Status Flags
/** Whether memory API is supported */
isSupported: boolean;
/** Whether monitoring is currently active */
isMonitoring: boolean;
/** Whether a memory leak is detected */
isLeakDetected: boolean;
/** Current severity level */
severity: Severity;
// Support Details
/** Level of API support */
supportLevel: SupportLevel;
/** List of available metrics */
availableMetrics: AvailableMetric[];
// Analysis Data
/** Memory history array (empty if history disabled) */
history: MemoryInfo[];
/** Memory usage trend */
trend: Trend;
/** Probability of memory leak (0-100) */
leakProbability: number;
// Actions
/** Start monitoring */
start: () => void;
/** Stop monitoring */
stop: () => void;
/** Take a named snapshot */
takeSnapshot: (id: string) => MemorySnapshot | null;
/** Compare two snapshots */
compareSnapshots: (id1: string, id2: string) => SnapshotDiff | null;
/** Clear history */
clearHistory: () => void;
/**
* Request garbage collection (hint only, not guaranteed).
*
* **Important limitations:**
* - In standard browsers, JavaScript cannot force GC. This is a hint only.
* - Only works reliably with Chrome/Node.js launched with `--expose-gc` flag.
* - The fallback memory pressure technique has no guaranteed effect.
* - V8 engine decides GC timing based on its own heuristics.
*
* Use this for debugging purposes, not for production memory management.
*/
requestGC: () => void;
// Formatting
/** Formatted memory values for display */
formatted: FormattedMemory;
}
/**
* Internal memory store state
*/
export interface MemoryStoreState {
memory: MemoryInfo | null;
domNodes: number | null;
eventListeners: number | null;
isMonitoring: boolean;
severity: Severity;
lastUpdated: number;
}
|