Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find memory used by a chrome tab using javascript

In a web application which loads large amount of data crashes when it exceeds a certain limit. So i want to find the memory used by a chrome tab using javascript ie by code to prevent this sort of problem.

like image 250
SHANib Avatar asked Apr 03 '16 06:04

SHANib


People also ask

How do I enable JavaScript memory in chrome?

Press Shift+Esc or go to the Chrome main menu and select More tools > Task manager to open the Task Manager. Right-click on the table header of the Task Manager and enable JavaScript memory. These two columns tell you different things about how your page is using memory:

How to monitor memory usage of Chrome extensions and tabs?

For this browser, there was always a keyboard shortcut available in the form of ‘Shift+Esc’ to display and monitor the memory usage by Chrome extensions and browser tabs in the Google Chrome Task Manager window.

What is memory in Chrome Developer Tools?

The Memory panel of the Chrome Developer Tools provides information about how a page is using memory. If you notice that your website has become slower, a memory leak should be considered as a possible cause. Chrome Memory tab is designed for solving memory issues, including debugging of JavaScript memory leaks.

How to check memory usage across browsers?

Protip: if you open the same page in IE, FF, Safari... and Chrome; and than navigate to about:memory in Chrome, it will report memory usage across all other browsers. Neat!


2 Answers

here from the source code of the chrome:

// Make the values returned to window.performance.memory more granular and more up to date in shared worker. Without this flag, the memory information is still available, but it is bucketized and updated less frequently. This flag also applys to workers. const char kEnablePreciseMemoryInfo[] = "enable-precise-memory-info";

So you need the --enable-precise-memory-info flag to get a more dynamic and accurate result. For security reasons, Google Chrome doesn't provide precise information via performance.memory by default. You can see here https://github.com/paulirish/memory-stats.js/tree/master a good way to monitor the memory. But this is not a good solution even if you have control over the way the user runs the "site". You can't be sure where Chrome will crash or how accurate is the data.

A first solution would be to make a global factory pattern that counts all the new objects and have some predefined size for each and when the size reaches a certain number you can alert the user. Be careful in that way you don't count the bound events. You can try and add the events in the same factory (or better another) but I don't know how accurate this will be.

A second solution, and better solution is this example. A great example, with 500,000 records with fast response and low memory. How? Easy, just load the data the user sees and nothing else. When the user scrolls it finds the right data without loading all the rest. That's something you can do with something that take many memory.

like image 184
Thomas Karachristos Avatar answered Oct 21 '22 05:10

Thomas Karachristos


We recently faced same problem.
You can use web performance API for to check current total memory allocation.
window.performance

It allows web pages to access to certain functions for measuring the performance of web page. There is a non-standard extension available only for chrome to record memory information.

window.performance.memory

It will return you following information.

  • usedJsHeapSize : total amount of memory being used by JS objects including V8 internal objects,
  • totalJsHeapSize : current size of the JS heap including free space not occupied by any JS objects

Also there is a js library available for recording memory stats. (https://github.com/spite/memory-stats.js)

Please checkout this as well. It might help you. - https://developer.chrome.com/devtools/docs/javascript-memory-profiling

like image 5
Ketan Avatar answered Oct 21 '22 07:10

Ketan