Skip to content

Commit

Permalink
Add method to take a save a JS memory heap snapshot
Browse files Browse the repository at this point in the history
Summary:
Changelog: [internal]

This adds support for taking JS memory heap snapshots from Fantom tests via `Fantom.saveJSMemoryHeapSnapshot`. This can be used in one-off tests to do memory analysis and determine the existence of leaks:

```
// Warm up

Fantom.saveJSMemoryHeapSnapshot('/path/to/my/1.heapsnapshot');

// Do work

Fantom.saveJSMemoryHeapSnapshot('/path/to/my/2.heapsnapshot');

// Clean up

Fantom.saveJSMemoryHeapSnapshot('/path/to/my/3.heapsnapshot');
```

Load these snapshots in Chrome and select "Objects allocated between 1 and 2" in the dropdown to see the potentially leaked objects.

In the future we could introduce additional utilities to analyze the snapshots and do the detection automatically, e.g.:

```
// Warm up

const baseline = Fantom.takeJSMemoryHeapSnapshot();

// Do work

const before = Fantom.takeJSMemoryHeapSnapshot();

// Clean up

const after = Fantom.takeJSMemoryHeapSnapshot();

const leaks = findMemoryLeaks(baseline, before, after);
expect(leaks.sizeKB()).toBeLessThan(THRESHOLD);
```

Differential Revision: D68953788
  • Loading branch information
rubennorte authored and facebook-github-bot committed Feb 3, 2025
1 parent bdbd0fa commit d93d68a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ if (typeof global.EventTarget === 'undefined') {
);
}

function saveJSMemoryHeapSnapshot(filePath: string): void {
if (getConstants().isRunningFromCI) {
throw new Error('Unexpected call to `saveJSMemoryHeapSnapshot` from CI');
}

NativeFantom.saveJSMemoryHeapSnapshot(filePath);
}

export default {
scheduleTask,
runTask,
Expand All @@ -282,4 +290,5 @@ export default {
flushAllNativeEvents,
unstable_benchmark: Benchmark,
scrollTo,
saveJSMemoryHeapSnapshot,
};
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ interface Spec extends TurboModule {
validateEmptyMessageQueue: () => void;
getRenderedOutput: (surfaceId: number, config: RenderFormatOptions) => string;
reportTestSuiteResultsJSON: (results: string) => void;
saveJSMemoryHeapSnapshot: (filePath: string) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>(
Expand Down

0 comments on commit d93d68a

Please sign in to comment.