Giter Site home page Giter Site logo

Comments (13)

hokiedsp avatar hokiedsp commented on May 29, 2024 1

@mhdawson -

a comment PR

If you could take care of it, that would be great. (I don't even know where to find the API documentation repo).

Most often it does not matter as when you are existing you are going to release all memory anyway.

As Node.js being a server-side script, this makes sense (scripts are running as daemon). I'm learning Node.js to develop an Electron app, so I should move on and pay attention to how the Electron performs GC.

Before I close, I have one more question. I'm going to assume that the finalize will never be called and manually destruct a native object, say with a myObject.Close() function. What is the proper procedure to release the native class instance from the wrapping Node object?

Looking at API doc, it appears that I need to call napi_remove_wrap() in native Close() implementation to do this. Is this all it takes? Also, is there a way to "reattach" the native object to that Node object?

Thanks

from abi-stable-node-addon-examples.

mhdawson avatar mhdawson commented on May 29, 2024

This line in Multiply I believe creates a new instance:

  status = napi_new_instance(env, cons, kArgCount, argv, &instance);

So I think that would explain 3 creates in addition to the one from var obj = new addon.MyObject(10);

In terms of no destruction, that should only happen when the JavaScript object is collected by the GC and there is no guarantee when that will happen or that it will happen before the program terminates.

from abi-stable-node-addon-examples.

hokiedsp avatar hokiedsp commented on May 29, 2024

@mhdawson - Thanks for your reply. Yeah, I've done a little more research on this destructer issue since and learned that the garbage collection is not running in node.js by design. Yikes.

Could the API documentation include a note on all the functions with napi_finalize argument that the callback may never get called?

FYI, I was able to explicitly destruct 2 instances that has assigned variable names obj and newobj by adding

delete obj;
delete newobj;

function forceGC() {
  if (global.gc) {
    global.gc();
  } else {
    console.warn('No GC hook! Start your program as `node --expose-gc ./addon.js`.');
  }
}

forceGC();

at the end of addon.js and run the example by --expose-gc ./addon. The other two apparently are lost completely.

from abi-stable-node-addon-examples.

mhdawson avatar mhdawson commented on May 29, 2024

@hokiedsp a comment would make sense. Do you want to submit a PR for that or should I?

In terms of the 2 that are not run, they would if the gc needed the space. Java had an option to run finalizers on exit, would have to look to see if V8 has the same. Most often it does not matter as when you are existing you are going to release all memory anyway.

from abi-stable-node-addon-examples.

yw662 avatar yw662 commented on May 29, 2024

btw, I have a question that, in the destructor of 6_object_wrap/napi,

void MyObject::Destructor(napi_env env, void* nativeObject, void* /*finalize_hint*/) {
  reinterpret_cast<MyObject*>(nativeObject)->~MyObject();
}

should we really do
native->~native();
instead of
delete native;
?

from abi-stable-node-addon-examples.

yw662 avatar yw662 commented on May 29, 2024

@mhdawson

Most often it does not matter as when you are exiting you are going to release all memory anyway.

Absolutely not. Remember nodejs is often used as daemons. People do not want daemons die.

from abi-stable-node-addon-examples.

bnoordhuis avatar bnoordhuis commented on May 29, 2024

What @mhdawson means is that the operating system reclaims memory when a process exits. There's no point in manual cleanup right before exit when the OS does it too (and is faster too.)

from abi-stable-node-addon-examples.

yw662 avatar yw662 commented on May 29, 2024

of course process release all resources when die, but, what I mean is, daemons never and should never die.

Do you cleanup memory leaks of systemd by kill and restart it?

In this case it is the end of a process, but it is actually only an end of a function, and may not be the end of processes in many cases.

Besides, the finalizers and destructors need to called. these might be filesystem or network or some other devices related, "even if it is the end of process". exp. you want your hard disk stop spinning when the hard disk driver exit, right ?

from abi-stable-node-addon-examples.

bnoordhuis avatar bnoordhuis commented on May 29, 2024

It sounds like you may be confusing two things. As long as Node.js is running the garbage collector tracks and reclaims objects; no special action is needed. @mhdawson's comment was strictly about process exit.

from abi-stable-node-addon-examples.

hokiedsp avatar hokiedsp commented on May 29, 2024

@yw662 - To your initial comment on Destruct, yeah, I agree it should read

delete reinterpret_cast<MyObject*>(nativeObject);

Otherwise, the allocated memory for nativeObject instance won't be returned to the heap. At least that's how I understand the process. link

from abi-stable-node-addon-examples.

noseratio avatar noseratio commented on May 29, 2024

Seems to be relevant to this thread, in the 6_object_wrap example, if you add a simple destructor it doesn't get called, even with delete and global.gc():

~MyObject()
{
  std::cout << "~MyObject";
}

It looks like ObjectWrap::FinalizeCallback doesn't get called but I haven't figured out why yet. For now I have to rely upon the manual MyObject.Close pattern.

Updated, interestingly if instead of doing delete obj I do obj = null, followed by global.gc(), then ~MyObject does get invoked.

from abi-stable-node-addon-examples.

fpelliccioni avatar fpelliccioni commented on May 29, 2024

@mhdawson - Thanks for your reply. Yeah, I've done a little more research on this destructer issue since and learned that the garbage collection is not running in node.js by design. Yikes.

Hello!

Do you have a link/reference where I can confirm your assertion? (I meant to "learned that the garbage collection is not running in node.js by design")

Thanks!

from abi-stable-node-addon-examples.

mhdawson avatar mhdawson commented on May 29, 2024

@hokiedsp I think this can be closed now. If I don't hear from you I'll go ahead and close it.

from abi-stable-node-addon-examples.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.