How do you free a wrapped C++ object when associated Javascript object is garbage collected in V8?

后端 未结 3 2015
攒了一身酷
攒了一身酷 2021-01-31 04:05

V8\'s documentation explains how to create a Javascript object that wraps a C++ object. The Javascript object holds on to a pointer to a C++ object instance. My question is, let

3条回答
  •  隐瞒了意图╮
    2021-01-31 05:06

    In general, if a garbage-collected language can hold references to resources outside of the language engine (files, sockets, or in your case C++ objects), you should provide a 'close' method to release that resource ASAP, no point waiting until the GC thinks it's worthwhile to destroy your object.

    it gets worse if your C++ object is memory-hungry and the garbage-collected object is just a reference: you might allocate thousands of objects, and the GC only sees a few KB's of tiny objects, not enough to trigger collection; while the C++ side is struggling with tens of megabytes of stale objects.

提交回复
热议问题