Comments (3)
It's important to understand what the output from MemLab means to debug memory leaks.
MemLab prints the retainer trace of the leaked objects. A retainer trace is a reference chain from the GC root (at the top of the trace) to the leaked object (at the bottom of the trace). It explains why and how a leaked object is still kept alive in memory.
To give a simple example, the following trace means A
keeps B
alive (with a reference named c
). B
keeps C
alive (with a reference named d
). Therefore C
cannot be garbage collected.
[A]
--c--> [B]
--d--> [C]
Now let's take a look at the leak trace from the tutorial:
[window](object) -> leakedObjects(property) -> [Array](object)
-> 0(element) -> [Detached HTMLDIVElement](native)
It means the window
object has a property named leakedObjects
pointing to an Array
in the web site under test in the tutorial. It is a contrived example just to demonstrate a leak example and what its leak trace looks like.
Therefore, unless your application contains a variable or object property named leakedObjects
, the leak trace detected from your web app will not include the leakedObjects
reference.
from memlab.
The traces from your web site do have some numbers, but they are not random numbers.
For instance, let's take a look at the first trace from your reports:
[] (synthetic) @1 [5.3MB]
--2 (shortcut)---> [Window / http://localhost:8000] (object) @9823 [48.6KB]
--require (property)---> [requirejs] (closure) @69911 [3.1KB]
--s (property)---> [Object] (object) @69913 [212 bytes]
--contexts (property)---> [Object] (object) @54171 [96 bytes]
--_ (property)---> [Object] (object) @54173 [2.3KB]
--defined (property)---> [Object] (object) @53929 [37.8KB]
--preact/compat (property)---> [Object] (object) @54089 [9.3KB]
--useTransition (property)---> [K] (closure) @137041 [68 bytes]
--context (internal)---> [] (object) @122325 [116.3KB]
--U (variable)---> [p] (object) @227695 [107.6KB]
--__P (property)---> [Detached HTMLElement] (native) @212991 [105.4KB]
--11 (element)---> [Detached InternalNode] (native) @262836032 [228 bytes]
--2 (element)---> [Detached DOMTokenList] (native) @213075 [84 bytes]
[] (synthetic) @1 [5.3MB] --2 (shortcut)---> [Window / http://localhost:8000]
means the GC root has a native shortcut reference (with index 2
) pointing to the Window
object.
However, most of the time you don't need to fully understand the entire trace to debug memory leaks. You only need to find the top-most detached DOM element and start working backwards:
The part that is particular interesting is this:
--_ (property)---> [Object] (object) @54173 [2.3KB]
--defined (property)---> [Object] (object) @53929 [37.8KB]
--preact/compat (property)---> [Object] (object) @54089 [9.3KB]
--useTransition (property)---> [K] (closure) @137041 [68 bytes]
--context (internal)---> [] (object) @122325 [116.3KB]
--U (variable)---> [p] (object) @227695 [107.6KB]
--__P (property)---> [Detached HTMLElement] (native) @212991 [105.4KB]
It seems that your code uses preact. This module uses a hook called useTransition
, which includes a callback that keeps a variable (@122325
) alive within its surrounding closure context.
Notice that I'm thinking out loud on how we debug memory leak traces without using the code links. In this case, however, it appears the website under test is minified; so investigating the code related to the leak based on the minified names are difficult.
To determine the actual function name of K
, the real variable name of U
, and the real constructor name of p
, you should run memlab on the un-minified version of your website. Once you have those actual names in your code, it's easy to search and narrow down to the code and files that generates this reference chain.
PS: If your website is un-minified but you are using a minfied preact library, it is likely that preact may have a memory leak.
from memlab.
The reason why Chrome DevTools can provide the code link but MemLab cannot is because each closure instance in the heap snapshot contains a scriptId
number, which is just a reference number for the JS source code loaded in Chrome's memory. Chrome DevTools can use this scriptId
key to map and query source code. However, Chrome does not save the information mapped by the scriptId
to disk, which means MemLab cannot query the mapped information based on scriptId
.
from memlab.
Related Issues (20)
- Question: Any idea about ignoring weak reference and circular reference in heap snapshot? HOT 7
- takeHeapSnapshot timed out HOT 10
- core_1.browserInfo.setPuppeteerConfig is not a function HOT 2
- Is there a way to run in scripting mode using silent? HOT 6
- Could not find Chrome (ver. 121.0.6167.85). This can occur if either 1. you did not perform an installation before running the script (e.g. `npx puppeteer browsers install chrome`) or 2. your cache path is incorrectly configured HOT 5
- Could not find Chrome (ver. 121.0.6167.85). This can occur if either 1. you did not perform an installation before running the script (e.g. `npx puppeteer browsers install chrome`) or 2. your cache path is incorrectly configured
- Keep `@memlab/*` versions in lockstep in package.json HOT 1
- how to handle Page crashed error ,when execute the run method。 HOT 1
- how to get leak info from memlab console HOT 6
- Clarify the documentation about measure mode HOT 3
- Using a basic `retainerReferenceFilter` causes Node OOM exception HOT 7
- How can I use script_id in IHeapLocation to get js url ? HOT 2
- Runtime exit code 1 during testInBrowser when running in AWS Lambda
- RangeError: Invalid string length in LeakTraceDetailsLogger HOT 3
- memlab find-leaks OOM HOT 6
- Memory Leak Issue in Grid Sample - No Leaks Found by MemLab HOT 1
- Add support for error logging only HOT 12
- "Heap out of memory" when using analyze plugins HOT 7
- devices is undefined when running memlabConfig.setDevice HOT 1
- JSON output HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memlab.