- Detached Document Object Model (DOM)-like objects in the React Native virtual DOM.
- Circular references in JavaScript objects.
- Retained components or event listeners to prevent garbage collection.
- Create sample React Native for Vega apps with deliberate memory leaks.
- Capture heap snapshots at different stages of the app’s execution.
- Use MemLab to analyze these snapshots and identify memory leaks.
- Interpret MemLab’s output to understand the nature and source of leaks.
Prerequisites
- Set up Node.js Install Node.js.
-
Install MemLab:
-
Configure your environment to prevent ANR crashes by using a debug vpkg build, or add
the following to your
react-native.config.js:
Step 1: Create a sample app
For testing purposes, create a sample app with deliberate sample leaks using the following command:Example app.js file
The following file shows code snippets from a sample app that demonstrates a memory leak scenario; and provides tools to create, clear, and analyze memory leaks.Step 2: Build your app
Run the following commands:Step 3: Run your app
-
Run the Vega Virtual Device.
-
Run your app on the Vega Virtual Device:
Step 4: Capture and copy heap snapshots
-
Before taking each snapshot, trigger garbage collection:
- While your app is running, click the Take Snapshot button to capture the heap snapshots on the device.
- Select the Create Detached DOM Leak button to generate 10000 memory-leaking elements.
- Retake a snapshot.
- Select the Clear Leaks button to remove the elements and simulate a memory leak.
-
Take a final snapshot.
When you select the Take Snapshot button, the logs show where the system saves the snapshot file.
- Copy the snapshot files to your local machine. With the heap snapshots you’ve taken, you can analyze and identify potential memory leaks.
Step 5: Run MemLab
To run MemLab on snapshot, use the following command format.- Loads all snapshots into memory.
- Compares the baseline and target snapshots for identification of new objects.
- Analyzes these new objects for persistence in the final snapshot.
- Traces object references for understanding of retention patterns.
MemLab command structure explained
The commandmemlab find-leaks is the core functionality of MemLab for identifying memory leaks.
The following commands are optional:
- - Specifies the initial heap snapshot taken before introducing any potential leak. It serves as the reference point.
- - Specifies the snapshot taken after introducing the potential leak. MemLab compares this snapshot against the baseline to identify new objects that might be leaks.
- - Specifies the snapshot taken after attempting to clear the leak. It helps MemLab determine if objects are truly leaking or just part of the normal app state.
--trace-all-objects- Tells MemLab to trace all objects, providing a more comprehensive analysis but potentially increasing processing time.
Example MemLab output
Understand the MemLab output
MemLab output has the following key elements.Alive objects allocated in the target page
This section displays objects still present in memory after app operations or changes. This list indicates potential memory retention issues and includes the following details for each object:name- The object name or its structural type.type- Whether the object is an ‘object’ (like JSObject, JSArray, etc.) or a ‘closure’ (a function that retains memory).count- The number of instances of each object type.retainedSize- The total memory retained by these objects.
JSArray (Object Type)- 305 instances, retaining 1.1MB of memory.JSObject(id, name)- 10,000 instances, also retaining 1.1MB of memory.
Sampling trace due to a large number of traces
This section shows that due to 10,100 traces, MemLab used 49.5% sampling for efficient processing.MemLab found X leaks
In the example, MemLab found five leaks. For each memory leak, the output lists contain:- Number of similar leaks - How many times the tool detected a similar leak pattern.
- Retained size of leaked objects - Total memory retained by the leaked objects.
- Leak trace - How objects interconnect and why the garbage collector can’t collect them.
Garbage Collection (GC) roots
These are the entry points for objects that remain in memory and prevent other objects from being garbage collected. The trace shows how GC roots keep objects alive through references, with paths leading back to JSObjects, JSArrays, and closure references. Example 1 Leak pattern: 4,956 similar leaks, retaining 1.1MB of memory.JSObject(current) → JSArray → JSObject(id, name).
This trace shows that the JSArray is keeping references to 10,000 JSObject(id, name) objects, preventing them from being garbage collected.
Example 2
Leak pattern: 22 similar leaks, retaining 2.9KB of memory.
Traces involve a Detached FiberNode RCTView object, which is part of React Native’s view system. Event handlers, like onBlur could cause the leak by unnecessarily retenting objects even after the code removes them.
Analyze the snapshot with MemLab
- Examine the Alive objects allocated in target page section to see retained objects.
-
Look at the MemLab found [X] leaks line to see how many distinct leaks MemLab identified.
For each leak:
- Note the number of similar leaks and total retained size.
- Examine the object trace to understand how the system retains objects.
- Pay attention to unexpected retentions, like detached DOM elements or large arrays.
Address leaks
You can address leaks in your app by performing the following actions:- Identify patterns in leaked objects (for example, detached DOM elements, uncleared event listeners).
- Locate the corresponding code in your app.
- Implement fixes, such as properly unmounting components, clearing arrays, or removing event listeners.
Verify fixes
You can verify fixes by performing the following actions:- Rebuild and rerun your app with the implemented fixes.
- Repeat the snapshot and analysis process.
- Confirm that the previously identified leaks are no longer present in the MemLab output.
Related topics
- 💬 Community: How to check if my app is running out of memory?
- Measure App KPIs
- Identify UI Rendering Issues
- Detect Overdraw
- Investigate Component Re-rendering Issues in React Native for Vega Apps

