Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doubt memory leak #3673

Closed
usol opened this issue Nov 5, 2015 · 3 comments
Closed

Doubt memory leak #3673

usol opened this issue Nov 5, 2015 · 3 comments
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. memory Issues and PRs related to the memory management or memory footprint. question Issues that look for answers.

Comments

@usol
Copy link

usol commented Nov 5, 2015

First I'm sorry for my english is not good.

I detect memory leak during aging test developed source.
I guess that occurred by node js.

My real source is can't upload because it is violate my company's security policy.
So I tested by modified node js test source(https://github.com/nodejs/node/tree/master/test/addons/async-hello-world).

Modified source is below.

  • binding.cc
#include <node.h>
#include <v8.h>
#include <uv.h>

struct async_req {
  uv_work_t req;
  int input;
  int output;
  v8::Isolate* isolate;
  v8::Persistent<v8::Function> callback;
};

void DoAsync(uv_work_t* r) {
  async_req* req = reinterpret_cast<async_req*>(r->data);
  //Sleep(1000);  // Simulate CPU intensive process...
  req->output = req->input * 2;
}

void AfterAsync(uv_work_t* r) {
  async_req* req = reinterpret_cast<async_req*>(r->data);
  v8::Isolate* isolate = req->isolate;
  v8::HandleScope scope(isolate);

  v8::Local<v8::Value> argv[2] = {
    v8::Null(isolate),
    v8::Integer::New(isolate, req->output)
  };

  v8::TryCatch try_catch(isolate);

  v8::Local<v8::Function> callback =
      v8::Local<v8::Function>::New(isolate, req->callback);
  callback->Call(isolate->GetCurrentContext()->Global(), 2, argv);

  // cleanup
  req->callback.Reset();
  delete req;

  if (try_catch.HasCaught()) {
    node::FatalException(isolate, try_catch);
  }
}

void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  v8::HandleScope scope(isolate);

  async_req* req = new async_req;
  req->req.data = req;

  req->input = args[0]->IntegerValue();
  req->output = 0;
  req->isolate = isolate;

  v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
  req->callback.Reset(isolate, callback);

  uv_queue_work(uv_default_loop(),
                &req->req,
                DoAsync,
                (uv_after_work_cb)AfterAsync);
}

void init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
  NODE_SET_METHOD(module, "exports", Method);
}

NODE_MODULE(binding, init);
  • binding.gyp
{
  'targets': [
    {
      'target_name': 'binding',
      'sources': [ 'binding.cc' ]
    }
  ]
}
  • test.js
'use strict';
var assert = require('assert');
var binding = require('./build/Release/binding');

invoke(0);

function invoke(cnt) {
    if (cnt < 100000) {
        binding(5, function(err, val) {
            global.gc();
            if (cnt % 100 === 0) {
                console.log(cnt / 100, process.memoryUsage().heapUsed);
            }
            process.nextTick(function() {
                invoke(++cnt);
            });
        });
    }

I run repeat 100,000 times binding addon, every time run gc and print memory usage when each 100th times.
Memory usage result is below.
image

I test on Windows 7 64bit, Node v.4.2.1. Node-gyp v.3.0.3.

Is it really memory leak? or do I use nodejs incorrectly?

@ChALkeR ChALkeR added the question Issues that look for answers. label Nov 5, 2015
@mscdex mscdex added c++ Issues and PRs that require attention from people who are familiar with C++. memory Issues and PRs related to the memory management or memory footprint. labels Nov 5, 2015
@mykelswitzer
Copy link

I am experiencing a similar issue on node4.2 running sails11, have tested and do not believe this is a sails issue.

@trevnorris
Copy link
Contributor

I'm failing to see where the leak is. From your own table Heap at Count 0 is the same as at Count 6615.

@bnoordhuis
Copy link
Member

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. memory Issues and PRs related to the memory management or memory footprint. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

6 participants