> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# Memory Usage Analysis

Memory analysis helps discover leaks and abnormal usage within Lynx pages and ensures stability. You can use Trace Memory Track and IDE tools, to comprehensively monitor and troubleshoot memory-related performance issues.

## Deep analysis with Trace

### Basic Page Memory Track (Lynx 3.4 And Later)

Starting from Lynx 3.4, Trace supports Memory Track, which shows Lynx page memory usage trends over time. Each Memory Track represents one Lynx page's memory changes.

![Basic page Memory Track](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/memory-track-latest.png)
Click the Memory Track curve to see the page's total memory usage and the memory used by Element, background scripting engine, main-thread scripting engine, images, and other available categories.

![Basic page Memory Track details](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/memory-track-details.png)
### Enhanced Memory Analysis

The complete workflow below is illustrated with Lynx 4.5. Memory data availability depends on the SDK, while the tracks, charts, and capture controls shown here also require a DevTool build that supports these features. The SDK version alone does not guarantee this UI. With an older SDK or DevTool, use the basic page Memory Track above for the available data.

Heap snapshot collection requires Lynx 4.5 or later and currently supports QuickJS only. See [Heap Snapshot Collection](/next/guide/performance/monitor-performance/heap-snapshot.md) for supported capture scopes and APIs.

#### Record Memory And Capture Snapshots

Enable memory data collection in the Lynx Trace panel. For active pages, use the camera button on the right to capture different types of snapshots. Snapshot capture supports only the QuickJS engine.

![Memory collection and snapshot capture in the Lynx Trace panel](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/trace/lynx-trace-panel.png)
#### Read Memory Tracks

The memory tracks show changes in page-instance memory, background scripting (BTS) VM memory, and process physical memory (PSS).

![Page, BTS VM, and process memory tracks](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/trace/lynx-trace-memory-track.png)
Click a BTS VM memory track to see its memory distribution at that moment, including memory used by each page sharing the VM, residuals from destroyed pages, and common scripts.

![BTS VM memory distribution at a selected moment](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/trace/lynx-memory-trace-click-vm.png)
Select a BTS VM memory track, or select a time range on it, to view an area chart of the VM's memory usage over that interval and identify pages with high memory usage or residuals.

![BTS VM memory area chart over a selected time range](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/trace/lynx-memory-trace-select-vm.png)
Click the PSS memory track to see memory usage for each Lynx VM and page at that moment.

![Lynx VM and page memory usage at a selected PSS observation](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/trace/lynx-memory-trace-click-pss-track.png)
#### Inspect Snapshot Details

Single-click a Snapshot icon on a track to view snapshots captured at different times and follow memory changes across the sequence.

![Snapshot sequence captured at different times](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/trace/lynx-memory-trace-click-snapshot.png)
Double-click a Snapshot icon on a track to open its details and analyze JavaScript object lifetimes and reference relationships.

![Snapshot details showing JavaScript objects and references](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/trace/lynx-memory-trace-view-snapshot.png)
## Analyze memory with IDEs

This section briefly introduces how to use Android Studio or Xcode’s built-in tools to debug apps and view performance metrics.

iOSAndroid

This section introduces how to analyze memory using Xcode.

### Analyze (static check)

Choose Product > Analyze or press Shift + Command + B to run automatically. Analyze uses syntax and memory context to find potential issues. It mainly detects four categories:

1. Logic errors: dereferencing null pointers, uninitialized variables.
2. Memory management errors: e.g., memory leaks.
3. Declaration errors: variables never used.
4. API call errors: missing libraries or frameworks.

:::tip
📌 Analyzer makes compiler-driven judgments and is not always accurate. If you see a report, review surrounding code; some cycle references that cause memory leaks cannot be identified by Analyzer. Analyzer makes compiler-driven judgments and is not always accurate. If you see a report, review surrounding code; some cycle references that cause memory leaks cannot be identified by Analyzer.
:::

### Leaks (dynamic check)

Xcode > Open Developer Tool > Instruments > Leaks

![Leaks dynamic check](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-12.png)
Choose Leaks, then specify the device and app to debug. In this example, an iPhone SE2 (iOS 14.5) simulator debugs the LynxExample app. Click the start button to check for memory leaks.

![Leaks Start Button](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-13.png)
After starting, operate in the simulator or on device. If the Leaks window shows issues, jump to the corresponding code to debug. You can pause to inspect memory info shown in the chart. After debugging, you can save the results locally.

![Leaks Analysis Result](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-14.png)
If a memory leak appears (example below), set the chart to Call Tree mode, then enable Invert Call Tree at the bottom of the window. Select any leak to jump to source code.

![Leaks Call Tree Mode](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-15.png)
### Allocations (detailed allocation)

Xcode > Open Developer Tool > Instruments > Allocations

![Allocations Tool](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-17.png)
Similar to Leaks but focuses on detailed memory allocations. The lower list shows per-method memory usage. Check a graph to show a bar chart above. Click an item to view stack trace details; click the right-hand Stack Trace pane to jump into code and inspect allocations.

![Allocations Stack Info](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-18.png)
The code section is highlighted and the allocation ratio is shown.

![Allocations](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-19.png)
###

This section introduces how to analyze memory using Android Studio.

### Profiler basics

![Profiler Tool](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-1.png)
First click the highlighted area in the toolbar; Android Studio performs a Build and then opens the Profiler window. From there you can run tests—click the ‘+’ icon to select a device connected via adb and the process to test.

![Profiler Choose Process](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-2.png)
Using a Xiaomi MIX (API 24), we run a \~45 s test and manually trigger a GC at 15 s. From left to right, the icons are GC, Capture Heap Dump, and manual Record (an option for devices below API 26 for memory analysis).

![Profiler Tool Record](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-3.png)
Over 45 s, the device’s memory changes are as follows:

![Profiler Tool Record](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-4.png)
In this example, after the manual GC, the app’s memory usage drops noticeably for a period.

Heap dumps show which objects are using memory at the time of capture. Especially after long user sessions, heap dumps reveal objects that should no longer be in memory, helping identify leaks. After clicking the Heap Dump icon, Java memory may temporarily increase because dumping runs in the same process and needs memory to collect data.

![Profiler Memory Status](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-5.png)
After capturing the heap dump, use the built-in analyzer. The table shows:

- Allocations: number of allocations in the heap.
- Native Size: total native memory used by this object type (Bytes). Visible on Android 7.0+ only. Some Java-allocated framework classNames (e.g., Bitmap) consume native memory.
- Shallow Size: total Java memory used by this object type (Bytes).
- Retained Size: total memory retained by all instances of this className (Bytes).

![Profiler Memory Detail](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-6.png)
Click a className in the chart to open the Instance List window, then jump to source for inspection.

![Profiler Source Info](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-7.png)
Or use the Reference window for more detailed checks and jump elsewhere for inspection.

![Profiler Source Transfer](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-8.png)
After capturing a heap dump, you can also export hprof data from the SESSIONS window via Export Heap Dump. Use other tools (such as MAT) to further analyze the heap.

![Profiler Memory Dump](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-9.png)
### Memory profiler

On devices below API 26, you need to manually record a time window before running memory performance analysis. Use the filter options (red circle) such as Arrange by className. In the table, select a className to jump to the Instance View, then to Allocation Call Stack to view stack traces and jump to source.

![Memory profiler](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-10.png)
### Symptoms of memory leaks

- Many Full GCs during runtime.
- During a specific thread’s execution, manual GC has little effect.
- Many allocations and frees in a short time (memory graph jitter).

![Memory profiler](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-11.png)
### Example leak and related analysis

```java
final className LoadedInChildClassLoader {

  static final byte[] moreBytesToLeak = new byte[1024 * 1024 * 100];

  private static final ThreadLocal<LoadedInChildClassLoader> threadLocal
          = new ThreadLocal<>();

  public LoadedInChildClassLoader() {
    threadLocal.set(this);
  }
}
```

#### Methods to trigger potential leaks

Apply pressure to app code and try to force leaks while cross-checking with the memory profiler. One way to induce leaks is to let the app run for some time, then inspect the heap; leaks may accumulate near the top of allocation. The smaller the leak, the longer the app needs to run to reveal it.

- Rotate the device repeatedly to trigger potential leaks.
- Switch between the debug app and other apps across different Activity states (e.g., go Home then return).

#### Possible causes of leaks

- Long-running threads.
- A thread loads a className via the ClassLoader.
- A className allocates large memory.
- A thread clears references to all custom classNames and the ClassLoader that loaded them.

## Performance testing with PerfDog

PerfDog is a third-party tool for checking Android and iOS app performance and is easy to use. After connecting a device and choosing the app to debug, click the red circled plus button at the bottom right to add performance parameters before starting.

![PerfDog Tool](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-20.png)
After the test, you can save data as a table and view performance parameters such as Jank count. Use the tool’s metrics as simple references to check app performance.

![PerfDog Save Data](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/plugin/static/ide-perf-21.png)