How To Find Objects From Memory Tab in Chrome

QASource Engineering Team | January 13, 2025

How To Find Objects From Memory Tab in Chrome

To find objects in the Memory tab of Chrome's Developer Tools, you can follow these steps:

  1. Open Developer Tools

    • Developer tools can be opened by Right-clicking on the webpage and then selecting "Inspect".
    • To open developer tools on Windows or Linux, use the keyboard shortcut 'Ctrl + Shift + I'; for Mac,d use Cmd + Option + c.
  2. Go to the Memory Tab under Developer tools

  3. Take a Heap Snapshot

    • In the Memory tab, you'll see options like "Heap snapshot," "Allocation instrumentation on the timeline," and "Allocation sampling."
    • Now the user has to click on the "Heap snapshot" button to take the snapshot. This captures the state of the JavaScript objects in memory.
  4. Analyze the Heap Snapshot

    • Once the snapshot is complete, you can explore the list of objects. The objects will be grouped under different categories like "Objects," "DOM elements," "Closures," etc.
    • You can use the search bar to find specific objects by name or type.
  5. Inspect Object Details

    • Click on an object to see more detailed information about it, including its properties, references, and memory size.
    • You can track where the object is being held in memory and find references that may be keeping it from being garbage collected.
  6. Filter and Search

    • Use the filter box to narrow down objects by their names or types.
    • Look for objects that may have unusual memory sizes or are not being released properly.
  7. Comparing Snapshots

    • You can take multiple snapshots and compare them to see how memory usage changes over time. This can help identify memory leaks or growing objects.

To automate finding objects in the Memory tab using a tool like Selenium or other browser automation tools, you might face challenges since browser automation tools do not typically have direct access to the Chrome DevTools. However, there are some workarounds you can use to automate this process:

Method 1: To use Chrome DevTools Protocol (CDP) in combination with Selenium

You can use Selenium with the Chrome DevTools Protocol (CDP). CDP allows you to interact with the browser in a way that isn't normally possible through Selenium alone, enabling access to performance and memory profiling features.

Here's a basic example using Python and Selenium with CDP:

  1. Set up Chrome Options to Use DevTools Protocol
    From selenium import webdriver
    
     options = webdriver.ChromeOptions()
     options.add_argument("--auto-open-devtools-for-tabs")  # Opens DevTools automatically
    
     driver = webdriver.Chrome(options=options)
     driver.get('https://example.com')
    
     # Connect to the DevTools Protocol
     devtools = driver.execute_cdp_cmd('Memory.enable', {})
  2. Taking a Heap Snapshot: You can use the CDP command to take a heap snapshot
    heap_snapshot = driver.execute_cdp_cmd('HeapProfiler.takeHeapSnapshot', {})
  3. Analyzing the Snapshot

    After taking the snapshot, you would parse the data to analyze it and find objects of interest. This data may be in JSON format, which you can programmatically analyze.

Method 2: Using Puppeteer (Node.js)

Puppeteer is another tool built on top of the Chrome DevTools Protocol, allowing easier manipulation of browser features. It is a Node.js library and is more suited to tasks like memory profiling and automation than Selenium.

Here is an example using Puppeteer to take a heap snapshot:

  1. Install Puppeteer
    npm install puppeteer
    
  2. Script to Take a Heap Snapshot
    const puppeteer = require('puppeteer');
     const fs = require('fs');
    
     (async () => {
     	const browser = await puppeteer.launch();
     	const page = await browser.newPage();
     	await page.goto('https://example.com');
    
     	const client = await page.target().createCDPSession();
     	await client.send('HeapProfiler.enable');
     	await client.send('HeapProfiler.takeHeapSnapshot');
    
     	console.log('Heap snapshot taken!');
     	await browser.close();
     })();
  3. Analyzing Heap Data: You can save the heap snapshot data to a file and analyze it using tools that support the Chrome DevTools snapshot format.

So, the user wants to automate memory analysis. In that case, it can't be done purely using Selenium, as it isn't built for deep integration with DevTools. Using it with CDP or switching to Puppeteer is more effective.

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment

Categories