<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marc</title>
    <description>The latest articles on DEV Community by Marc (@mg132406560).</description>
    <link>https://dev.to/mg132406560</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F536068%2F8b31ea21-6b24-41c7-af30-a0eaceb5a522.gif</url>
      <title>DEV Community: Marc</title>
      <link>https://dev.to/mg132406560</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mg132406560"/>
    <language>en</language>
    <item>
      <title>Check your NodeJS program memory utilization</title>
      <dc:creator>Marc</dc:creator>
      <pubDate>Tue, 08 Dec 2020 08:53:57 +0000</pubDate>
      <link>https://dev.to/mg132406560/check-your-nodejs-program-memory-utilization-fmk</link>
      <guid>https://dev.to/mg132406560/check-your-nodejs-program-memory-utilization-fmk</guid>
      <description>&lt;p&gt;As a NodeJS developer, I regularly want to control how much memory is used by the programs I’m creating, so I can assess my code choices and thus keep, update or totally change the way I coded some functionalities.&lt;/p&gt;

&lt;p&gt;I try to force myself to do it as much as possible. I think the best version of code is the one that fulfills the requirements with the minimum of resource utilization.&lt;/p&gt;

&lt;p&gt;I can do it using some NodeJS built-in functions, such as process.hrtime (&lt;a href="https://nodejs.org/api/process.html#process_process_hrtime_time"&gt;https://nodejs.org/api/process.html#process_process_hrtime_time&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;But this will add several lines of code and output values to the console.&lt;/p&gt;

&lt;p&gt;I prefer to use a small NPM library that will do all the stuff for me and also present the result in a readable graphic chart.&lt;/p&gt;

&lt;p&gt;For example, let’s say I want to check the memory utilization when populating then deleting an array (this is a very simple case just to present what can be done). Here is the small program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const randomstring = require('randomstring')

let array=[]
for (let index = 0; index &amp;lt; 10000; index++) {
    array[index]=randomstring.generate(32)
}

for (let index = 0; index &amp;lt; 10000; index++) {
    array.splice(index)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I want now to check the memory utilization of such program. It will updated, using memuse NPM package (&lt;a href="https://www.npmjs.com/package/memuse):"&gt;https://www.npmjs.com/package/memuse):&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memuse=require('memuse')
const randomstring = require('randomstring')

memuse.init('./mem.csv')

let array=[]
for (let index = 0; index &amp;lt; 10000; index++) {
    array[index]=randomstring.generate(32)
    memuse.poll()
}

memuse.tag('start deletion')

for (let index = 0; index &amp;lt; 10000; index++) {
    array.splice(index)
    memuse.poll()
}

memuse.end('./mem.svg')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the program above, here are the explained steps: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I loaded the memuse library&lt;/li&gt;
&lt;li&gt;I initialized it (memory utilization statistics will go into the mem.csv file)&lt;/li&gt;
&lt;li&gt;I called the memuse.poll() function each time I wanted to record the memory utilization statistics&lt;/li&gt;
&lt;li&gt;Just after the array is loaded and before deleting it, I added a tag to locate this step into the chart, using memuse.tag() &lt;/li&gt;
&lt;li&gt;Finally the memuse.end() produces the graphical chart.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I run the program and I get this nice memory utilization chart:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L7kooLGD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/98fgiam4ajta6ztnbzy4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L7kooLGD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/98fgiam4ajta6ztnbzy4.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this article will help you. Thanks.&lt;/p&gt;

</description>
      <category>node</category>
      <category>memory</category>
      <category>chart</category>
    </item>
  </channel>
</rss>
