title: Javascript heap out of memory while running a js script to fetch data from an api every minute- javascript/node.js
body: <p>My program grabs ~70 pages of 1000 items from an API and bulk-inserts it into a SQLite database using Sequelize. After looping through a few times, the memory usage of node goes up to around 1.2GB and and then eventually crashes the program with this error: <code>FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory</code>. I've tried using <code>delete</code> for all of the big variables that I use for the response of the API call and stuff with <code>variable = undefined</code> and then <code>global.gc()</code>, however I still get huge amounts of memory usage and eventually it crashes. Would increasing the memory cap of Node.js help? Or would the memory usage of it just keep increasing until it hits the next cap?</p>
  <p>Here's the full output of the error:</p>
  <pre><code>
  &lt;--- Last few GCs ---&gt;
  
  [6760:0x128008000]   436085 ms: Scavenge 4068.7 (4110.5) -&gt; 4068.7 (4110.5) MB, 2.7 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436345 ms: Scavenge 4073.0 (4113.8) -&gt; 4072.9 (4118.8) MB, 9.2 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436565 ms: Scavenge (reduce) 4079.1 (4122.1) -&gt; 4079.3 (4121.9) MB, 4.6 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  
  
  &lt;--- JS stacktrace ---&gt;
  
  
  &lt;--- Last few GCs ---&gt;
  
  [6760:0x128008000]   436085 ms: Scavenge 4068.7 (4110.5) -&gt; 4068.7 (4110.5) MB, 2.7 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436345 ms: Scavenge 4073.0 (4113.8) -&gt; 4072.9 (4118.8) MB, 9.2 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436565 ms: Scavenge (reduce) 4079.1 (4122.1) -&gt; 4079.3 (4121.9) MB, 4.6 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  
  
  &lt;--- JS stacktrace ---&gt;
  
  FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
  FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
  
  &lt;--- Last few GCs ---&gt;
  
  [6760:0x128008000]   436085 ms: Scavenge 4068.7 (4110.5) -&gt; 4068.7 (4110.5) MB, 2.7 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436345 ms: Scavenge 4073.0 (4113.8) -&gt; 4072.9 (4118.8) MB, 9.2 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436565 ms: Scavenge (reduce) 4079.1 (4122.1) -&gt; 4079.3 (4121.9) MB, 4.6 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  
  
  &lt;--- JS stacktrace ---&gt;
  
  FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
  
  &lt;--- Last few GCs ---&gt;
  
  [6760:0x128008000]   436085 ms: Scavenge 4068.7 (4110.5) -&gt; 4068.7 (4110.5) MB, 2.7 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436345 ms: Scavenge 4073.0 (4113.8) -&gt; 4072.9 (4118.8) MB, 9.2 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  [6760:0x128008000]   436565 ms: Scavenge (reduce) 4079.1 (4122.1) -&gt; 4079.3 (4121.9) MB, 4.6 / 0.0 ms  (average mu = 0.918, current mu = 0.875) allocation failure 
  
  
  &lt;--- JS stacktrace ---&gt;
  
  FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
  zsh: segmentation fault  npm start
  </code></pre>
  <p>I'm running node v15.12.0 on a MacBook Air M1 with 16GB of ram, however I don't think its the hardware that's the issue here as far as I understand this... Does anyone know why this is happening? Thanks in advance :)</p>
  <p>Edit:</p>
  <p>Turns out one of the node modules I was using never removed it's responses from the api calls, so I just rewrote that section of my code and now I'm good to go.</p>