-
Notifications
You must be signed in to change notification settings - Fork 465
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
Add memory mangament feature #286
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# MemoryManagement | ||
|
||
The `MemoryManagement` class contains functions that give the JavaScript engine | ||
an indication of the amount of externally allocated memory that is kept alive by | ||
JavaScript objects. | ||
|
||
## Methods | ||
|
||
### AdjustExternalMemory | ||
|
||
The function `AdjustExternalMemory` adjusts the amount of registered external | ||
memory. Used to give the JavaScript engine an indication of the amount of externally | ||
allocated memory that is kept alive by JavaScript objects. | ||
The JavaScript engine uses this to decide when to perform global garbage collections. | ||
Registering externally allocated memory will trigger global garbage collections | ||
more often than it would otherwise in an attempt to garbage collect the JavaScript | ||
objects that keep the externally allocated memory alive. | ||
|
||
```cpp | ||
static int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in_bytes); | ||
``` | ||
|
||
- `[in] env`: The environment in which the API is inoked under. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "in[v]oked" |
||
- `[in] change_in_bytes`: The change in externally allocated memory that is kept | ||
alive by JavaScript objects expressed in bytes. | ||
|
||
Returns the adjusted memory value. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
Value externalAllocatedMemory(const CallbackInfo& info) { | ||
int64_t kSize = 1024 * 1024; | ||
int64_t baseline = MemoryManagement::AdjustExternalMemory(info.Env(), 0); | ||
int64_t tmp = MemoryManagement::AdjustExternalMemory(info.Env(), kSize); | ||
tmp = MemoryManagement::AdjustExternalMemory(info.Env(), -kSize); | ||
return Boolean::New(info.Env(), tmp == baseline); | ||
} | ||
|
||
Object InitMemoryManagement(Env env) { | ||
Object exports = Object::New(env); | ||
exports["externalAllocatedMemory"] = Function::New(env, externalAllocatedMemory); | ||
return exports; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a newline to the end of the file! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
assert.strictEqual(binding.memory_management.externalAllocatedMemory(), true) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"[It is ]
Uused ..."