File tree Expand file tree Collapse file tree 10 files changed +79
-1
lines changed
Expand file tree Collapse file tree 10 files changed +79
-1
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,7 @@ still a work in progress as its not yet complete).
9090 - [ ArrayBuffer] ( doc/array_buffer.md )
9191 - [ TypedArray] ( doc/typed_array.md )
9292 - [ TypedArrayOf] ( doc/typed_array_of.md )
93+ - [ Memory Management] ( doc/memory_management.md )
9394 - [ Async Operations] ( doc/async_operations.md )
9495 - [ AsyncWorker] ( doc/async_worker.md )
9596 - [ Promises] ( doc/promises.md )
Original file line number Diff line number Diff line change 1+ # MemoryManagement
2+
3+ The ` MemoryManagement ` class contains functions that give the JavaScript engine
4+ an indication of the amount of externally allocated memory that is kept alive by
5+ JavaScript objects.
6+
7+ ## Methods
8+
9+ ### AdjustExternalMemory
10+
11+ The function ` AdjustExternalMemory ` adjusts the amount of registered external
12+ memory used to give the JavaScript engine an indication of the amount of externally
13+ allocated memory that is kept alive by JavaScript objects.
14+ The JavaScript engine uses this to decide when to perform global garbage collections.
15+ Registering externally allocated memory will trigger global garbage collections
16+ more often than it would otherwise in an attempt to garbage collect the JavaScript
17+ objects that keep the externally allocated memory alive.
18+
19+ ``` cpp
20+ static int64_t MemoryManagement::AdjustExternalMemory (Env env, int64_t change_in_bytes);
21+ ```
22+
23+ - `[in] env`: The environment in which the API is invoked under.
24+ - `[in] change_in_bytes`: The change in externally allocated memory that is kept
25+ alive by JavaScript objects expressed in bytes.
26+
27+ Returns the adjusted memory value.
Original file line number Diff line number Diff line change @@ -3224,6 +3224,17 @@ inline void AsyncWorker::OnWorkComplete(
32243224 delete self;
32253225}
32263226
3227+ // //////////////////////////////////////////////////////////////////////////////
3228+ // Memory Management class
3229+ // //////////////////////////////////////////////////////////////////////////////
3230+
3231+ inline int64_t MemoryManagement::AdjustExternalMemory (Env env, int64_t change_in_bytes) {
3232+ int64_t result;
3233+ napi_status status = napi_adjust_external_memory (env, change_in_bytes, &result);
3234+ NAPI_THROW_IF_FAILED (env, status, 0 );
3235+ return result;
3236+ }
3237+
32273238// These macros shouldn't be useful in user code.
32283239#undef NAPI_THROW
32293240#undef NAPI_THROW_IF_FAILED
Original file line number Diff line number Diff line change @@ -76,6 +76,8 @@ namespace Napi {
7676 // / Defines the signature of a N-API C++ module's registration callback (init) function.
7777 typedef Object (*ModuleRegisterCallback)(Env env, Object exports);
7878
79+ class MemoryManagement ;
80+
7981 // / Environment for N-API values and operations.
8082 // /
8183 // / All N-API values and operations must be associated with an environment. An environment
@@ -1549,6 +1551,12 @@ namespace Napi {
15491551 std::string _error;
15501552 };
15511553
1554+ // Memory management.
1555+ class MemoryManagement {
1556+ public:
1557+ static int64_t AdjustExternalMemory (Env env, int64_t change_in_bytes);
1558+ };
1559+
15521560} // namespace Napi
15531561
15541562// Inline implementations of all the above class methods are included here.
Original file line number Diff line number Diff line change 88 " Anna Henningsen (https://github.com/addaleax)" ,
99 " Arunesh Chandra (https://github.com/aruneshchandra)" ,
1010 " Benjamin Byholm (https://github.com/kkoopa)" ,
11- " Cory Mickelson (https://github.com/corymickelson)" ,
11+ " Cory Mickelson (https://github.com/corymickelson)" ,
1212 " David Halls (https://github.com/davedoesdev)" ,
1313 " Eric Bickle (https://github.com/ebickle)" ,
1414 " Gabriel Schulhof (https://github.com/gabrielschulhof)" ,
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ Object InitError(Env env);
1313Object InitExternal (Env env);
1414Object InitFunction (Env env);
1515Object InitHandleScope (Env env);
16+ Object InitMemoryManagement (Env env);
1617Object InitName (Env env);
1718Object InitObject (Env env);
1819Object InitPromise (Env env);
@@ -33,6 +34,7 @@ Object Init(Env env, Object exports) {
3334 exports.Set (" function" , InitFunction (env));
3435 exports.Set (" name" , InitName (env));
3536 exports.Set (" handlescope" , InitHandleScope (env));
37+ exports.Set (" memory_management" , InitMemoryManagement (env));
3638 exports.Set (" object" , InitObject (env));
3739 exports.Set (" promise" , InitPromise (env));
3840 exports.Set (" typedarray" , InitTypedArray (env));
Original file line number Diff line number Diff line change 1313 'external.cc' ,
1414 'function.cc' ,
1515 'handlescope.cc' ,
16+ 'memory_management.cc' ,
1617 'name.cc' ,
1718 'object/delete_property.cc' ,
1819 'object/get_property.cc' ,
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ let testModules = [
1919 'external' ,
2020 'function' ,
2121 'handlescope' ,
22+ 'memory_management' ,
2223 'name' ,
2324 'object/delete_property' ,
2425 'object/get_property' ,
Original file line number Diff line number Diff line change 1+ #include " napi.h"
2+
3+ using namespace Napi ;
4+
5+ Value externalAllocatedMemory (const CallbackInfo& info) {
6+ int64_t kSize = 1024 * 1024 ;
7+ int64_t baseline = MemoryManagement::AdjustExternalMemory (info.Env (), 0 );
8+ int64_t tmp = MemoryManagement::AdjustExternalMemory (info.Env (), kSize );
9+ tmp = MemoryManagement::AdjustExternalMemory (info.Env (), -kSize );
10+ return Boolean::New (info.Env (), tmp == baseline);
11+ }
12+
13+ Object InitMemoryManagement (Env env) {
14+ Object exports = Object::New (env);
15+ exports[" externalAllocatedMemory" ] = Function::New (env, externalAllocatedMemory);
16+ return exports;
17+ }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const buildType = process . config . target_defaults . default_configuration ;
3+ const assert = require ( 'assert' ) ;
4+
5+ test ( require ( `./build/${ buildType } /binding.node` ) ) ;
6+ test ( require ( `./build/${ buildType } /binding_noexcept.node` ) ) ;
7+
8+ function test ( binding ) {
9+ assert . strictEqual ( binding . memory_management . externalAllocatedMemory ( ) , true )
10+ }
You can’t perform that action at this time.
0 commit comments