Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9104691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:55:33+00:00 2026-06-17T01:55:33+00:00

I have an application where I am sequentially downloading mp3 files from a server,

  • 0

I have an application where I am sequentially downloading mp3 files from a server, storing them in my server temporarily, then streaming them directly to clients, like so:

function downloadNextTrack(){
  var request = http.get('http://mp3server.com', function(response){
    response.on('data', function(data) {
      fs.appendFile('sometrack.mp3', data, function (err) {});
    });
    response.on('end', function(){
      streamTrack('sometrack.mp3');
    }
  });
};

var clients = []; // client response objects are pushed to this array when they request the stream through a route like /stream.mp3

var stream;

function streamTrack(track){
  stream = fs.createReadStream(track);
  stream.on('data', function(data){
    clients.forEach(function(client) {
      client.write(data);
    });
  });
  stream.on('end', function(){
    downloadNextTrack(); // redoes the same thing with another track
  }
};

Apparently this code is creating a lot of buffers which aren’t being freed by the OS, when I run ‘free -M’ command, this is what I get (after about 4 hours of running the app):

                   total      used       free     shared    buffers     cached
              Mem: 750        675         75          0         12        180
-/+ buffers/cache:            481        269
             Swap: 255        112        143

The number under ‘buffers’ constantly rises (as well as the cached memory) and the OS apparently doesn’t reclaim that 180mb back, until eventually my app runs out of memory and crashes when I try spawning a small process to verify a track’s bitrate, sampling rate, id3 info, etc.

I have diagnosed with a lot of different tools (such as memwatch and nodetime) to find out if it was an internal memory leak and it isn’t, the V8 memory heap as well as the Node RSS vary +/- 10mb but stay constant for most part while the OS free memory gets lower and lower (when the Node process starts I have about 350MB of free memory).

I read somewhere that Buffer instances allocated by Node have direct access to raw memory and so V8 doesn’t have power over them (which checks out with the fact that I am not getting memory leaks from the V8 heap), the thing is, I need a way to get rid of these old buffers. Is this possible? Or will I have to restart my app every 5 hours or so (or worse, buy more RAM!)?

PS. I am running Node v0.8.16 on Ubuntu 10.04.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:55:34+00:00Added an answer on June 17, 2026 at 1:55 am

    I agree with Tiago,
    I think this is caused because of the recursive nature of your code.
    I don’t think the streams is what gobbling up your heap, because as you said, the stream variable is being reassigned with a new ReadStream with every iteration.
    However, the http.get’s request and response (and whatever Buffers they use) in line 2 are never being released before calling the next iteration; they are scoped within the downloadNextTrack function. You end up with a recursive stack trace that has a set of request and response objects (and some underlying buffers) per file.

    In general, if this code needs to run many, many times, why not opt-out of the recursion and do it all iteratively? a never-ending recursion will always gobble more and more memory, until the program crashes, even if there’s no memory leaks on your part.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several points regarding .NET socket implementation so I will state them sequentially:
I have some simple questions, I have a client-server application and data sent on
I have a small application that process a large quantity of (relatively small) files.
I have application which uses Sherlock ActionBar package. The application uses platform-specific behavior for
I have application where i have two view controllers my first view and second
I have application with needs to have access to some sensitive data(in this case
I have application which needs to use a dll (also written by me) which
I have application that makes different queries with different results so the caching in
I have Application settings stored under HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany branch. Settings must be same for different
I have Application written with GWT 1.7. I have one page where I upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.