Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gathering JavaScript memory profile data from users

I'm writing a client-heavy site. Since my own testing will only get me so far, I'd like to gather some statistics on how it's performing in the wild.

I'm imagining adding some sort of profiling code to my app which will run some percentage of the time (so it doesn't slow everyone down) and sending that info home.

Adding some timing benchmarks should be easy, but what really becomes a problem with long-running pages with lots of JS is memory usage. Is there a way to instrument the memory used by my app from normal, unprivileged JS code in any of the major browsers? Are there any other good profiling metrics that are available?

like image 956
Peeja Avatar asked May 02 '13 13:05

Peeja


People also ask

Can JavaScript access memory?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

Does JavaScript have memory leaks?

JavaScript code can experience memory leaks by keeping hidden references to objects. Hidden references can cause memory leaks in many unexpected ways.

How JavaScript store variables in memory?

“Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap. A stack is usually a continuous region of memory allocating local context for each executing function. Heap is a much larger region storing everything allocated dynamically.


1 Answers

In Chrome:

for (var key in performance.memory) {
    alert(key+': '+performance.memory[key]);
}

DEMO: http://jsfiddle.net/usuXV/1/

Sample output:

jsHeapSizeLimit: 1620000000
usedJSHeapSize: 10000000
totalJSHeapSize: 16100000

You can also use console.memory. Seems to return the same results.

like image 146
Pedro L. Avatar answered Oct 06 '22 22:10

Pedro L.