title: Linux kill my process for out of memory, how to see how much I use?
body: <p>Following my previous question (<a href="https://stackoverflow.com/questions/18589674/prevent-my-node-js-app-to-be-killed-by-the-os">Prevent my node.js app to be killed by the OS</a>) I have remade my gestion of how I download the files.</p>
  
  <p>To make it short, I need to download some video files (test files are 3 video, mp4, ~3min), I was downloading them all at once, the process was getting killed and dmesg was saying (after 2 download completed):</p>
  
  <pre><code>Out of memory: kill process (node) score 824 or sacrifice child
  Killed process (node)...
  </code></pre>
  
  <p>So I retried by downloading the video one after another, but at the second download, I get the exact same message, and my program get killed.</p>
  
  <p>Is there some way to see which part of my code go wrong, or to prevent linux from killing my process?</p>
  
  <p>BTW, isn t three simultaneous download eat up more memory than one? So why do I get kill faster on the one-after-another?</p>
  
  <p>The app is running on a raspberry pi, no GUI, raspbian, and I believe to be the only app running (other than system process)</p>
  
  <p>EDIT:
  Some details about the function and how I think it would work now:
  The app is started in node, not in any browser.
  Since it download the video one after another, I was thinking it would eat less memory, but it seems to be wrong since I download more data while firing all at once, and the reason is stop seems to be the same.</p>
  
  <p>Here the download function, in case it could help:</p>
  
  <p>file_url is of type <a href="http://adress.com/rpi/test.mp4" rel="nofollow noreferrer">http://adress.com/rpi/test.mp4</a></p>
  
  <pre><code>function download (file_url, callback){
      var option={host:url.parse(file_url).host, port:80, path:url.parse(file_url).pathname};
      var file_name=url.parse(file_url).pathname.split('/').pop();
      var file=fs.createWriteStream(DOWNLOAD_DIR+file_name);
      //Seems to crash while here:
      http.get(options, function(res){
          res.on('data', function(data){
              file.write(data);
          }).on('end'), function(){
              file.end();
              callback(DOWNLOAD_DIR+file_name);
          });
      });
  };
  </code></pre>
  
  <p>I m sure this function do work since I did download some files with it successfully.</p>
  
  <p>Here how I made the download follow:
  (
  This function may not work completely right, I do not ask for debug, I never get to end it.)</p>
  
  <pre><code>function download_all (list, callback){
      var i=0;
      function follow(){
          i=i+1;
          if (i&lt;list.length){
              download(DOWNLOAD_ADD+list[i], follow);
          }
      }
      download(DOWNLOAD_ADD+list[0], follow);
  }
  </code></pre>
  
  <p>EDIT2:</p>
  
  <p>Since I still didn t found a way to core dump or the other solution proposed by Roman, I used process.memoryUsage to print it while downloading.</p>
  
  <p>heapTotal is slowly increasing all along the process, between 20 000 000 and 25 000 000, with some drop. heapUsed is increasing quickly, and drop regulary to ~2 000 000 when going higher than 6 000 000, with some spike at ~12 000 000.</p>
  
  <p>When the process got killed, the last print was:
  rss: 428 707 840, heapTotal: 23 842 176, heapUsed: 5 854 164</p>
  
  <p>I m note sure of how to read it yet, but it seems to me that the process was far from eating all the memory...</p>
  
  <p>I m still reading docs about core dumping and /proc/</p>
  
  <p>EDIT3:</p>
  
  <p>My tech director suggested that the download was maybe cached then writed the entire file on the disk, wich could explain the memory usage. It don t really stick with the all-at-once case since I could end the download of 2 video and get killed while it ended the third, but I m now searching about that too</p>
  
  <p>EDIT4:</p>
  
  <p>Nevermind last edit, I was already writing by chunk, wich give us the question about why the memory get ate up</p>