Skip to content

Memory leak: NetworkManager/NetworkEventManager retained via CdpPage during navigation test #208

Description

@ARiedijk

Hi with some duration tests I found perhaps a memory leak. I have included the heap dump (Analyse it with MAT tool from Eclipse) and added a patch with possible solution.

Environment

  • jvppeteer: 3.4.1
  • JDK: AppClassLoader (JDK 17+), jdk.internal.loader.ClassLoaders$AppClassLoader @ 0x603c010a0
  • OS: Windows 10/11 (likely), Maven build

Description

  • One instance of com.ruiyun.jvppeteer.cdp.core.NetworkManager (AppClassLoader @ 0x603c010a0) occupies 1,184,336 bytes (12.33%).
  • Memory is accumulated in one instance of com.ruiyun.jvppeteer.cdp.core.NetworkEventManager (AppClassLoader @ 0x603c010a0), occupying 1,182,440 bytes (12.31%).
  • The main thread (java.lang.Thread @ 0x603c00820) holds a local reference to com.ruiyun.jvppeteer.cdp.core.CdpPage @ 0x603c1d240, which is on the shortest path to
    NetworkEventManager @ 0x603dbcdc0.
  • Main thread locals total 56,696 bytes (0.59%); CdpPage @ 0x603c1d240 retains 4,528 bytes (0.05%).
Image

Significant frame and locals

  • com.ruiyun.jvppeteer.TestNavigatePage.givenLocalFile_whenGoto_thenOk()V (TestNavigatePage.java:190)
  • Stacktrace with involved locals is available (see attached in original context).

Steps to Reproduce

  1. Run TestNavigatePage.givenLocalFile_whenGoto_thenOk() (line 190).
  2. After 2 hours do a heap dump (e.g., jcmd GC.heap_dump file=heap.hprof).
  3. Analyze with MAT.
  4. Observe NetworkManager → NetworkEventManager retained via CdpPage from the main thread.

Expected
After page/test cleanup, NetworkManager/NetworkEventManager and related listeners/maps are released.

Actual
NetworkEventManager remains strongly reachable via CdpPage, accumulating ~1.18 MB; NetworkManager totals ~1.18 MB.

Initial Analysis / Suspected Cause
Likely unremoved event listeners/subscriptions, scheduled tasks, or request-tracking maps in NetworkEventManager referenced by NetworkManager/CdpPage. Disposal/teardown
is not clearing them, leaving a live reference chain from the test method’s CdpPage.

Request / Proposed Fix

  • Please review and verify the fix in:
    • com.ruiyun.jvppeteer.cdp.core.NetworkEventManager
    • com.ruiyun.jvppeteer.cdp.core.NetworkManager
  • Ensure:
    • All listeners/subscriptions are removed on page/connection close.
    • Internal maps (e.g., request tracking) are cleared.
    • Any executors/timers are shut down.
    • CdpPage.close() (and test teardown) cascade disposal to NetworkManager/NetworkEventManager.

Fix_memory_leak_in_NetworkManager.patch

heapdump-1757338330966-actions4.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions