Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

im fairly new to node.js and using node_postgres libray. I've been using sockjs and Primus as transport for my web server prototype. I used PM2 as my monitoring tool on my server and noticed that the ram usage goes up as i query postgres. I'm using the node_postgres built in connection pool for the database. Disconnecting the client from the server does not flush out the memory and this just keeps compounding the more clients I connect and the more queries that I do. How do I flush out this memory as the clients disconnect from my server? I'd appreciate any inputs from you guys! here is my code:

var Static = require('node-static');  
var http = require('http');
var file = new Static.Server('./public');
var PORT = 8083;

var pg = require('pg');
var conString = "postgres://username:password@localhost/database";

/*********** Primus Plugins ***************/
var Primus = require('primus');
var Emitter = require('primus-emitter');

var server = http.createServer(function (request, response) {  
    request.addListener('end', function () {
    file.serve(request, response);
   }).resume();
 }).listen(PORT);




// primus instance
var primus = new Primus(server, { transformer: 'sockjs', parser: 'JSON' });

// add emitter to Primus
primus.use('emitter', Emitter);




primus.on('connection', function (spark) {


  console.log("User: " + spark.id + " connected");


  spark.on('test', function (data){

    pg.connect(conString, function(err, client, done) {
     if(err) {
       return console.error('error fetching client from pool', err);
     }

     client.query('SELECT * from tbldata where dev_id = ' + data, function(err, result) {




    if(err) {
      return console.error('error running query', err);
    }

   console.log(result);
   spark.send('done_query', result);

   //call `done()` to release the client back to the pool
   done();
   });

 });



  });

});

primus.save(__dirname +'/primus.js');
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.