About Us
IDKBlogs

  All  
Javascript
NodeJS
AngularJS
Angular2+
ReactJS
Others

Detect the memory uses of node app | Use of -inspect | Profiling Nodejs Application

  idkblogs.com      September 22, 2019

Shubham Verma

Shubham Verma


Detect the memory uses of node app | Use of -inspect | Profiling Nodejs Application

T

oday, We’ll learn how we can know our node app better, and how to detect how much memory our app is consuming. So let’s follow the below steps.

Step 1: Create a basic node.js application:

Let’s create a basic node.js application, create two files with the name app.js and package.json:

package.json


                        {
                            "name": "heap-snapshot",
                            "version": "1.0.0",
                            "private": true,
                            "scripts": {
                                   "start": "node --inspect app.js"
                              },
                            "author": "Shubham Verma",
                            "license": "",
                            "dependencies": {
                                             "express": "^4.14.1",
                                             "fast-levenshtein": "^2.0.6"
                                 }
                            }
                    

app.js:

Step 2: Go to the app location and run below command

npm install
Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

Step 3: After the successful installation, run bellow command:

node --inspect app.js
Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

Step 4: Be careful at this step, after running the `node — inspect app.js` command you can see an URL start with ‘ws’, just like in above snapshot

ws://127.0.0.1:9229/a1c6b690-d8ef-4da6–9a50–23118d400640

Now what you need to do is, you need to make an URL like this

devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=127.0.0.1:9229/a1c6b690-d8ef-4da6–9a50–23118d400640

Step 4: Now, open the above URL in the browser, make sure URL is correct, otherwise, you will get the following error:

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

( Suggestion: make your URL into browser’s address bar )
If your URL is correct you can see below the page in your browser:

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

Step 5: Click on the link ‘http://localhost:8080/’:

Step 5: Click on the link ‘http://localhost:8080/’, it will open a tab with URL ‘http://localhost:8080/’ ( you can directly open the URL `http://localhost:8080/` into browser )

Step 6: After clicking/opening URL `http://localhost:8080/` you can see the below page, also open network tab in dev tools.

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

You can see there are many requests are hitting by the browser. if not then check all the steps again ( you are doing wrong somewhere ) If yes then move to the next step.

Step 7:Now open the dev tool tab ( which is previously opened in step 4 )

Now open the dev tool tab ( which is previously opened in step 4 ) Click on “Memory”, Select “heap snapshot” under the “select profiling type” and click on the below button “Take snapshot”.

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

Step 8:After clicking the “Take snapshot”:

After clicking the “Take snapshot”, you can see there is a snapshot taken under the “profiles-> HEAP SNAPSHOT”. Now click on that “snapshot 1".

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App


Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

Step 9:After clicking on that “Snapshot 1”, You can see that each and every object created for this app.

* You can also see the num of that variable which is created to run this app.

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

* You can see how much memory is being used for this app.

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

You can see in the above snapshot, the app is consuming total of 8.3 MB memory.

* You can see the “Shallow size” of that kind of variables.

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

* You can see the “Retained Size” of that kind of variables.

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

You can take multiple snapshots and compare each and everything by clicking on the left button as shown in the below snapshot:

Profiling Nodejs Application: Detect the memory uses of node app | Use of -inspect

Profiling Node App

The best use of it (Profiling Node App):

You can take snapshots before hitting an API and after hitting “N” API, Now you can compare the differences between. You can check where you need to improve. In the next blog, I will tell you how you can detect the memory leaks in your node app and how you can get the execution time of each function in your node app, also I will tell you how you can create the “flame chart” for your node app.

Congratulations… You are becoming an expert in node.js


Thank you

for taking the time to read this article. If you’re interested in Node.js or JavaScript this link will help you a lot.



Copyright © 2020 IDKBlogs | All Right Reserved

About Us

Powered by : IDKBlogs

Last Update : 19/09/2020