Investigate Node.js Memory Leak issue in Linux App Service

Published Sep 19 2021 07:54 PM 2,291 Views
Microsoft

When running your Node.js application in Azure Linux App Service, you may encounter memory consumption issue.

 

Nodejs heapdump module provides developers a simple mechanism for producing V8 heap snapshots for later inspection. In this article, we will talk about:

  • Instrumenting your app with heapdump
  • Take Node.js memory heap dump in Linux App Service
  • Use Google Chrome Developer tools to compare heap dump files, so we can efficiently locate memory leak issue in the Node.js application

 

Instrumenting your app with heapdump

  1. Install the "heapdump" module
    In general, we just need to use npm to install the "heapdump" module
    npm install heapdump

    For your Node.js application going to deploy to Azure App Service, we can define it in your package.json "dependencies" section like the following: 

    1.png

     

  2. Once installed, you need to load the heapdump module into your application
    For example, in my Node Express project, I put the following line in my app.js file. 
    var heapdump = require('heapdump')
     2.png
    If you are going to run this application in Linux OS (like Linux App Service), no other code change required for heap dump.
    But if you need to run it in Windows OS, you will have to explicitly use the following code to take heap snapshot.
    heapdump.writeSnapshot()​

 

Take Node.js memory heap dump in Linux App Service

After you deployed your Node.js Application with heapdump installed and loaded, you can startup and keep your Linux App Service run as normal.
When you need to investigate a memory leak issue, we can take multiple heap snapshots in a sequence of time.

 

  1. Login to your Node.js Linux App Service WEBSSH console. 
    https://.scm.azurewebsites.net/webssh/host

     

  2. Find your Node.js application process ID
    # ps aux | grep node
    Hanli_Ren_0-1630737389677.png

     

  3. Take the first Snapshot as the base line.
    We can use SIGUSR2 signal to trigger the heapdump module to capture a snapshot.
    Use the following command to take a heap snapshot:
    # kill -SIGUSR2 <node_pid>
    Hanli_Ren_0-1630737537662.png

     

    Snapshots are written to your node application working directory (/home/site/wwwroot) with a timestamp like heapdump-xxxxxxx.xxxxxx.heapsnapshot.Hanli_Ren_1-1630738376455.png

    Hanli_Ren_2-1630737642663.png

     

  4. Run load test on you App Service for a while to let the memory usage grow. Then take the second Snapshot.Hanli_Ren_3-1630737918024.pngHanli_Ren_4-1630738602082.png

     

    Hanli_Ren_5-1630737935628.png

     

  5. Download your heap dump files to your local machine.
    We can go to https://.scm.azurewebsites.net/newui/fileManager# to download the filesHanli_Ren_6-1630737978555.png

 

Use Google Chrome Developer tools to compare heap dump files

  1. In you local machine, open your Google Chrome Browser.
    Then use F12 to open Chrome Dev Tools.
    Load the heapdump-xxxxxxx.xxxxxx.heapsnapshot files into Chrome Dev Tools by going to the Memory tab, right-clicking the Profiles pane and selecting Load:

    Hanli_Ren_0-1630738285493.png

     

  2. Select the second heapdump file (the one captured later), then use "Comparison" to compare it with the first heapdump file (the one captured earlier)
     

    Hanli_Ren_2-1630738392309.png

     

  3. We can sort the result by "#Delta" or "Size Delta" to see which type of object increased dramatically.
    Then we can find those objects with highest Allocation Size.
    Hanli_Ren_3-1630738457677.pngIn the above example, we can see an Array consumed large amount of memory.
    This is because of my example memory leak code keep on appending the Array with a random string "tVXn8LhhAn8….."
    	var express = require('express');
    	var router = express.Router();
    	const numeral = require('numeral');
    	
    	var garbage = ['g1'];
    	
    	function generategarbage() {
    	  for (let i = 0; i < 10000; i++) {
    	    garbage.push("tVXn8LhhAn8wjjbD6wPnsb8UAKAVJHqCVXhu6ochaMI9sMnjWHw1WqMvwKyUvXzdCcU3A4dPuaWKLnI3ZTFTUgQ1BY33yGSWxGRA0RfYmqgtPkZxqp2ErjG0Uzle2npbmFWIsQeP8XTLPL3phqusfisswxvZafAx6XsKmNrPeVjSPTgoFeoe9tOUlriRxRtTOSXgXOOcGO9aWlkrA2pFI9d71R9bgn3xlpgJ7zKUMyi4lHPY2MCQxKGFAyk7DiW3sklLn5ePLEYQI0Q8Cd345lXknjsITfLBbe0UFMlzXdvdPerZpCdtnKdo4opZFy7xQOnjxQICmgxRs5J45PiKMPxOCqb5s1OBhPid2gT2pQibMWpUL6W7xGMNt4oWXpnNhQASEAnIEOt6aKhLncnrHM12pyUAdhqVXzNVmh5q7hNmFbeOL6iEoUrewuKWOTIgNdWK13n98y5kIQ69Gsqa12GKtq2pQGaP4kRkkNdc4GkAU9X9QftfAkui31WjNoaiXFFjQOgwcy70ukPQB5m180HBQr5OiyGhCG9JSmP1wkFpZNEhUsCN7XlxWWYVkh3fdcPauzS2vZtVyBlC9qupcLzOSzQVcMaHkAHDB0oHVMoVSXAnZlTB1i1TRi01grHQrNuUEWSEW7mpqdq3cVzpoU0eYNs7kydb9qaIx2UhSjzLy3zjqcw6IC7e0nMfSAce2relcYORjiIjZcwh73fYDckEVaxzhn0L5CftTXyZaZX9OmsL9gJOtBxPW5GqQjgqlCoPhh4kh3eLC9W3ZN1cF69EXMAkh6Z6YN0rjDgf5eSWxazqFCL7fWWRAlJoZadFhgPlXa6AiGtP18SeBuvnPPgsIEdowhblHBSReVrcJJBeGIIPJpebS6MF89q8y3np05jzmkPJzx4bABhaoc9tyjxJIRLm67HDiqqJkCEx6rrlG3NtgdQkmwDFItkRHHyyppMVphiL9oXRvSdQUX2EQe03wQ8lSXzANiNxSUeq");
    	  }
    	}
    	
    	/* GET home page. */
    	router.get('/', function(req, res, next) {
    	  generategarbage();
    	  res.status(200).send(`Array appended.`);
    	});
    module.exports = router;​

     

  4. By clicking on any of those objects, we can dig deeper to check the "Retainers" details.
    It can help you figure out why the memory leak objects cannot be cleaned up by V8 GC, and trace back to the related piece of code.
     
    Hanli_Ren_0-1630738698107.png

     

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2719258%22%20slang%3D%22en-US%22%3EInvestigate%20Node.js%20Memory%20Leak%20issue%20in%20Linux%20App%20Service%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2719258%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20running%20your%20Node.js%20application%20in%20Azure%20Linux%20App%20Service%2C%20you%20may%20encounter%20memory%20consumption%20issue.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENodejs%20%3CA%20href%3D%22https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fheapdump%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Eheapdump%3C%2FA%3E%20module%20provides%20developers%20a%20simple%20mechanism%20for%20producing%20V8%20heap%20snapshots%20for%20later%20inspection.%20In%20this%20article%2C%20we%20will%20talk%20about%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EInstrumenting%20your%20app%20with%20heapdump%3C%2FLI%3E%0A%3CLI%3ETake%20Node.js%20memory%20heap%20dump%20in%20Linux%20App%20Service%3C%2FLI%3E%0A%3CLI%3EUse%20Google%20Chrome%20Developer%20tools%20to%20compare%20heap%20dump%20files%2C%20so%20we%20can%20efficiently%20locate%20memory%20leak%20issue%20in%20the%20Node.js%20application%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId--355994072%22%20id%3D%22toc-hId--351488939%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-2131518761%22%20id%3D%22toc-hId-2136023894%22%3EInstrumenting%20your%20app%20with%20heapdump%3C%2FH2%3E%0A%3COL%3E%0A%3CLI%3EInstall%20the%20%22heapdump%22%20module%20%3CBR%20%2F%3EIn%20general%2C%20we%20just%20need%20to%20use%20npm%20to%20install%20the%20%22heapdump%22%20module%20%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Enpm%20install%20heapdump%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EFor%20your%20Node.js%20application%20going%20to%20deploy%20to%20Azure%20App%20Service%2C%20we%20can%20define%20it%20in%20your%20package.json%20%22dependencies%22%20section%20like%20the%20following%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%221.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308089i0E4897053825C9CB%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%221.png%22%20alt%3D%221.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3EOnce%20installed%2C%20you%20need%20to%20load%20the%20heapdump%20module%20into%20your%20application%3CBR%20%2F%3EFor%20example%2C%20in%20my%20Node%20Express%20project%2C%20I%20put%20the%20following%20line%20in%20my%20app.js%20file.%26nbsp%3B%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Evar%20heapdump%20%3D%20require('heapdump')%3C%2FCODE%3E%3C%2FPRE%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308090i089884E927902767%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222.png%22%20alt%3D%222.png%22%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3EIf%20you%20are%20going%20to%20run%20this%20application%20in%20Linux%20OS%20(like%20Linux%20App%20Service)%2C%20no%20other%20code%20change%20required%20for%20heap%20dump.%3CBR%20%2F%3EBut%20if%20you%20need%20to%20run%20it%20in%20Windows%20OS%2C%20you%20will%20have%20to%20explicitly%20use%20the%20following%20code%20to%20take%20heap%20snapshot.%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eheapdump.writeSnapshot()%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH2%20id%3D%22toc-hId-324064298%22%20id%3D%22toc-hId-328569431%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1483390165%22%20id%3D%22toc-hId--1478885032%22%3ETake%20Node.js%20memory%20heap%20dump%20in%20Linux%20App%20Service%3C%2FH2%3E%0A%3CP%3EAfter%20you%20deployed%20your%20Node.js%20Application%20with%20heapdump%20installed%20and%20loaded%2C%20you%20can%20startup%20and%20keep%20your%20Linux%20App%20Service%20run%20as%20normal.%20%3CBR%20%2F%3EWhen%20you%20need%20to%20investigate%20a%20memory%20leak%20issue%2C%20we%20can%20take%20multiple%20heap%20snapshots%20in%20a%20sequence%20of%20time.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ELogin%20to%20your%20Node.js%20Linux%20App%20Service%20WEBSSH%20console.%26nbsp%3B%20%3CBR%20%2F%3Ehttps%3A%2F%2F.scm.azurewebsites.net%2Fwebssh%2Fhost%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3EFind%20your%20Node.js%20application%20process%20ID%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%23%20ps%20aux%20%7C%20grep%20node%3C%2FCODE%3E%3C%2FPRE%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_0-1630737389677.png%22%20style%3D%22width%3A%20883px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308094i97D031ECC2DBE279%2Fimage-dimensions%2F883x77%3Fv%3Dv2%22%20width%3D%22883%22%20height%3D%2277%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_0-1630737389677.png%22%20alt%3D%22Hanli_Ren_0-1630737389677.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3ETake%20the%20first%20Snapshot%20as%20the%20base%20line.%3CBR%20%2F%3EWe%20can%20use%20%3CSTRONG%3ESIGUSR2%3C%2FSTRONG%3E%20signal%20to%20trigger%20the%20heapdump%20module%20to%20capture%20a%20snapshot.%20%3CBR%20%2F%3EUse%20the%20following%20command%20to%20take%20a%20heap%20snapshot%3A%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%23%20kill%20-SIGUSR2%20%3CNODE_PID%3E%3C%2FNODE_PID%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_0-1630737537662.png%22%20style%3D%22width%3A%20845px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308095iD64F79367CB9C145%2Fimage-dimensions%2F845x20%3Fv%3Dv2%22%20width%3D%22845%22%20height%3D%2220%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_0-1630737537662.png%22%20alt%3D%22Hanli_Ren_0-1630737537662.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3ESnapshots%20are%20written%20to%20your%20node%20application%20working%20directory%20(%2Fhome%2Fsite%2Fwwwroot)%20with%20a%20timestamp%20like%20heapdump-xxxxxxx.xxxxxx.heapsnapshot.%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_1-1630738376455.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308103iA627B7B83587A253%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_1-1630738376455.png%22%20alt%3D%22Hanli_Ren_1-1630738376455.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_2-1630737642663.png%22%20style%3D%22width%3A%20827px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308097iAE744E1506C44351%2Fimage-dimensions%2F827x162%3Fv%3Dv2%22%20width%3D%22827%22%20height%3D%22162%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_2-1630737642663.png%22%20alt%3D%22Hanli_Ren_2-1630737642663.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3ERun%20load%20test%20on%20you%20App%20Service%20for%20a%20while%20to%20let%20the%20memory%20usage%20grow.%20Then%20take%20the%20second%20Snapshot.%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_3-1630737918024.png%22%20style%3D%22width%3A%20841px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308098iD612BA65C073CF12%2Fimage-dimensions%2F841x19%3Fv%3Dv2%22%20width%3D%22841%22%20height%3D%2219%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_3-1630737918024.png%22%20alt%3D%22Hanli_Ren_3-1630737918024.png%22%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_4-1630738602082.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308106i00B60D7897E94BA6%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_4-1630738602082.png%22%20alt%3D%22Hanli_Ren_4-1630738602082.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_5-1630737935628.png%22%20style%3D%22width%3A%20817px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308100iE0031A906A56E02F%2Fimage-dimensions%2F817x284%3Fv%3Dv2%22%20width%3D%22817%22%20height%3D%22284%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_5-1630737935628.png%22%20alt%3D%22Hanli_Ren_5-1630737935628.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3EDownload%20your%20heap%20dump%20files%20to%20your%20local%20machine.%20%3CBR%20%2F%3EWe%20can%20go%20to%20https%3A%2F%2F.scm.azurewebsites.net%2Fnewui%2FfileManager%23%20to%20download%20the%20files%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_6-1630737978555.png%22%20style%3D%22width%3A%20731px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308101i523F77716AF9A406%2Fimage-dimensions%2F731x533%3Fv%3Dv2%22%20width%3D%22731%22%20height%3D%22533%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_6-1630737978555.png%22%20alt%3D%22Hanli_Ren_6-1630737978555.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1004122668%22%20id%3D%22toc-hId-1008627801%22%3EUse%20Google%20Chrome%20Developer%20tools%20to%20compare%20heap%20dump%20files%3C%2FH2%3E%0A%3COL%3E%0A%3CLI%3EIn%20you%20local%20machine%2C%20open%20your%20Google%20Chrome%20Browser.%3CBR%20%2F%3EThen%20use%20F12%20to%20open%20Chrome%20Dev%20Tools.%3CBR%20%2F%3ELoad%20the%20heapdump-xxxxxxx.xxxxxx.heapsnapshot%20files%20into%20Chrome%20Dev%20Tools%20by%20going%20to%20the%20%3CSTRONG%3EMemory%3C%2FSTRONG%3E%20tab%2C%20right-clicking%20the%20Profiles%20pane%20and%20selecting%20Load%3A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_0-1630738285493.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308102iEA2B0B3A605FFE13%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_0-1630738285493.png%22%20alt%3D%22Hanli_Ren_0-1630738285493.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3ESelect%20the%20second%20heapdump%20file%20(the%20one%20captured%20later)%2C%20then%20use%20%22Comparison%22%20to%20compare%20it%20with%20the%20first%20heapdump%20file%20(the%20one%20captured%20earlier)%3CDIV%20id%3D%22tinyMceEditorHanli_Ren_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_2-1630738392309.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308104i028EF775D5583884%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_2-1630738392309.png%22%20alt%3D%22Hanli_Ren_2-1630738392309.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3EWe%20can%20sort%20the%20result%20by%20%22%23Delta%22%20or%20%22Size%20Delta%22%20to%20see%20which%20type%20of%20object%20increased%20dramatically.%3CBR%20%2F%3EThen%20we%20can%20find%20those%20objects%20with%20highest%20Allocation%20Size.%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_3-1630738457677.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308105i2DD85EFF12F7DAB1%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_3-1630738457677.png%22%20alt%3D%22Hanli_Ren_3-1630738457677.png%22%20%2F%3E%3C%2FSPAN%3EIn%20the%20above%20example%2C%20we%20can%20see%20an%20Array%20consumed%20large%20amount%20of%20memory.%3CBR%20%2F%3EThis%20is%20because%20of%20my%20example%20memory%20leak%20code%20keep%20on%20appending%20the%20Array%20with%20a%20random%20string%20%22tVXn8LhhAn8%E2%80%A6..%22%20%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3E%20var%20express%20%3D%20require('express')%3B%0A%20var%20router%20%3D%20express.Router()%3B%0A%20const%20numeral%20%3D%20require('numeral')%3B%0A%20%0A%20var%20garbage%20%3D%20%5B'g1'%5D%3B%0A%20%0A%20function%20generategarbage()%20%7B%0A%20%20%20for%20(let%20i%20%3D%200%3B%20i%20%26lt%3B%2010000%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20garbage.push(%22tVXn8LhhAn8wjjbD6wPnsb8UAKAVJHqCVXhu6ochaMI9sMnjWHw1WqMvwKyUvXzdCcU3A4dPuaWKLnI3ZTFTUgQ1BY33yGSWxGRA0RfYmqgtPkZxqp2ErjG0Uzle2npbmFWIsQeP8XTLPL3phqusfisswxvZafAx6XsKmNrPeVjSPTgoFeoe9tOUlriRxRtTOSXgXOOcGO9aWlkrA2pFI9d71R9bgn3xlpgJ7zKUMyi4lHPY2MCQxKGFAyk7DiW3sklLn5ePLEYQI0Q8Cd345lXknjsITfLBbe0UFMlzXdvdPerZpCdtnKdo4opZFy7xQOnjxQICmgxRs5J45PiKMPxOCqb5s1OBhPid2gT2pQibMWpUL6W7xGMNt4oWXpnNhQASEAnIEOt6aKhLncnrHM12pyUAdhqVXzNVmh5q7hNmFbeOL6iEoUrewuKWOTIgNdWK13n98y5kIQ69Gsqa12GKtq2pQGaP4kRkkNdc4GkAU9X9QftfAkui31WjNoaiXFFjQOgwcy70ukPQB5m180HBQr5OiyGhCG9JSmP1wkFpZNEhUsCN7XlxWWYVkh3fdcPauzS2vZtVyBlC9qupcLzOSzQVcMaHkAHDB0oHVMoVSXAnZlTB1i1TRi01grHQrNuUEWSEW7mpqdq3cVzpoU0eYNs7kydb9qaIx2UhSjzLy3zjqcw6IC7e0nMfSAce2relcYORjiIjZcwh73fYDckEVaxzhn0L5CftTXyZaZX9OmsL9gJOtBxPW5GqQjgqlCoPhh4kh3eLC9W3ZN1cF69EXMAkh6Z6YN0rjDgf5eSWxazqFCL7fWWRAlJoZadFhgPlXa6AiGtP18SeBuvnPPgsIEdowhblHBSReVrcJJBeGIIPJpebS6MF89q8y3np05jzmkPJzx4bABhaoc9tyjxJIRLm67HDiqqJkCEx6rrlG3NtgdQkmwDFItkRHHyyppMVphiL9oXRvSdQUX2EQe03wQ8lSXzANiNxSUeq%22)%3B%0A%20%20%20%7D%0A%20%7D%0A%20%0A%20%2F*%20GET%20home%20page.%20*%2F%0A%20router.get('%2F'%2C%20function(req%2C%20res%2C%20next)%20%7B%0A%20%20%20generategarbage()%3B%0A%20%20%20res.status(200).send(%60Array%20appended.%60)%3B%0A%20%7D)%3B%0Amodule.exports%20%3D%20router%3B%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3EBy%20clicking%20on%20any%20of%20those%20objects%2C%20we%20can%20dig%20deeper%20to%20check%20the%20%22%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fthlorenz%2Fv8-perf%2Fblob%2Fmaster%2Fmemory-profiling.md%23retainers%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3ERetainers%3C%2FA%3E%22%20details.%3CBR%20%2F%3EIt%20can%20help%20you%20figure%20out%20why%20the%20memory%20leak%20objects%20cannot%20be%20cleaned%20up%20by%20V8%20GC%2C%20and%20trace%20back%20to%20the%20related%20piece%20of%20code.%20%3CBR%20%2F%3E%3CDIV%20id%3D%22tinyMceEditorHanli_Ren_4%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_0-1630738698107.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308107i40EE46992895090E%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_0-1630738698107.png%22%20alt%3D%22Hanli_Ren_0-1630738698107.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2719258%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ENode.js%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Sep 03 2021 11:58 PM
Updated by: