| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4c3e965 commit 78be0d0
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1127,6 +1127,22 @@ is unknown, `undefined` is returned. | |||
| 1127 | 1127 | See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more | |
| 1128 | 1128 | information. | |
| 1129 | 1129 | ||
| 1130 | + ## `process.availableMemory()` | ||
| 1131 | + | ||
| 1132 | + <!-- YAML | ||
| 1133 | + added: REPLACEME | ||
| 1134 | + --> | ||
| 1135 | + | ||
| 1136 | + > Stability: 1 - Experimental | ||
| 1137 | + | ||
| 1138 | + * {number} | ||
| 1139 | + | ||
| 1140 | + Gets the amount of free memory that is still available to the process | ||
| 1141 | + (in bytes). | ||
| 1142 | + | ||
| 1143 | + See [`uv_get_available_memory`][uv_get_available_memory] for more | ||
| 1144 | + information. | ||
| 1145 | + | ||
| 1130 | 1146 | ## `process.cpuUsage([previousValue])` | |
| 1131 | 1147 | ||
| 1132 | 1148 | <!-- YAML | |
@@ -4026,6 +4042,7 @@ cases: | |||
| 4026 | 4042 | [process_warning]: #event-warning | |
| 4027 | 4043 | [report documentation]: report.md | |
| 4028 | 4044 | [terminal raw mode]: tty.md#readstreamsetrawmodemode | |
| 4045 | + [uv_get_available_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_available_memory | ||
| 4029 | 4046 | [uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory | |
| 4030 | 4047 | [uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t | |
| 4031 | 4048 | [wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -178,6 +178,7 @@ const rawMethods = internalBinding('process_methods'); | |||
| 178 | 178 | process.resourceUsage = wrapped.resourceUsage; | |
| 179 | 179 | process.memoryUsage = wrapped.memoryUsage; | |
| 180 | 180 | process.constrainedMemory = rawMethods.constrainedMemory; | |
| 181 | + process.availableMemory = rawMethods.availableMemory; | ||
| 181 | 182 | process.kill = wrapped.kill; | |
| 182 | 183 | process.exit = wrapped.exit; | |
| 183 | 184 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1883,25 +1883,6 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) { | |||
| 1883 | 1883 | should_abort_on_uncaught_toggle_.Deserialize(ctx); | |
| 1884 | 1884 | } | |
| 1885 | 1885 | ||
| 1886 | - uint64_t GuessMemoryAvailableToTheProcess() { | ||
| 1887 | - uint64_t free_in_system = uv_get_free_memory(); | ||
| 1888 | - size_t allowed = uv_get_constrained_memory(); | ||
| 1889 | - if (allowed == 0) { | ||
| 1890 | - return free_in_system; | ||
| 1891 | - } | ||
| 1892 | - size_t rss; | ||
| 1893 | - int err = uv_resident_set_memory(&rss); | ||
| 1894 | - if (err) { | ||
| 1895 | - return free_in_system; | ||
| 1896 | - } | ||
| 1897 | - if (allowed < rss) { | ||
| 1898 | - // Something is probably wrong. Fallback to the free memory. | ||
| 1899 | - return free_in_system; | ||
| 1900 | - } | ||
| 1901 | - // There may still be room for swap, but we will just leave it here. | ||
| 1902 | - return allowed - rss; | ||
| 1903 | - } | ||
| 1904 | - | ||
| 1905 | 1886 | void Environment::BuildEmbedderGraph(Isolate* isolate, | |
| 1906 | 1887 | EmbedderGraph* graph, | |
| 1907 | 1888 | void* data) { | |
@@ -2006,7 +1987,7 @@ size_t Environment::NearHeapLimitCallback(void* data, | |||
| 2006 | 1987 | static_cast<uint64_t>(old_gen_size), | |
| 2007 | 1988 | static_cast<uint64_t>(young_gen_size + old_gen_size)); | |
| 2008 | 1989 | ||
| 2009 | - uint64_t available = GuessMemoryAvailableToTheProcess(); | ||
| 1990 | + uint64_t available = uv_get_available_memory(); | ||
| 2010 | 1991 | // TODO(joyeecheung): get a better estimate about the native memory | |
| 2011 | 1992 | // usage into the overhead, e.g. based on the count of objects. | |
| 2012 | 1993 | uint64_t estimated_overhead = max_young_gen_size; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -216,6 +216,11 @@ static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) { | |||
| 216 | 216 | } | |
| 217 | 217 | } | |
| 218 | 218 | ||
| 219 | + static void GetAvailableMemory(const FunctionCallbackInfo<Value>& args) { | ||
| 220 | + uint64_t value = uv_get_available_memory(); | ||
| 221 | + args.GetReturnValue().Set(static_cast<double>(value)); | ||
| 222 | + } | ||
| 223 | + | ||
| 219 | 224 | void RawDebug(const FunctionCallbackInfo<Value>& args) { | |
| 220 | 225 | CHECK(args.Length() == 1 && args[0]->IsString() && | |
| 221 | 226 | "must be called with a single string"); | |
@@ -633,6 +638,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data, | |||
| 633 | 638 | SetMethod(isolate, target, "umask", Umask); | |
| 634 | 639 | SetMethod(isolate, target, "memoryUsage", MemoryUsage); | |
| 635 | 640 | SetMethod(isolate, target, "constrainedMemory", GetConstrainedMemory); | |
| 641 | + SetMethod(isolate, target, "availableMemory", GetAvailableMemory); | ||
| 636 | 642 | SetMethod(isolate, target, "rss", Rss); | |
| 637 | 643 | SetMethod(isolate, target, "cpuUsage", CPUUsage); | |
| 638 | 644 | SetMethod(isolate, target, "resourceUsage", ResourceUsage); | |
@@ -674,6 +680,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |||
| 674 | 680 | registry->Register(RawDebug); | |
| 675 | 681 | registry->Register(MemoryUsage); | |
| 676 | 682 | registry->Register(GetConstrainedMemory); | |
| 683 | + registry->Register(GetAvailableMemory); | ||
| 677 | 684 | registry->Register(Rss); | |
| 678 | 685 | registry->Register(CPUUsage); | |
| 679 | 686 | registry->Register(ResourceUsage); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -641,13 +641,8 @@ static void PrintResourceUsage(JSONWriter* writer) { | |||
| 641 | 641 | writer->json_keyvalue("constrained_memory", constrained_memory); | |
| 642 | 642 | } | |
| 643 | 643 | ||
| 644 | - // See GuessMemoryAvailableToTheProcess | ||
| 645 | - if (!err && constrained_memory && constrained_memory >= rss) { | ||
| 646 | - uint64_t available_memory = constrained_memory - rss; | ||
| 647 | - writer->json_keyvalue("available_memory", available_memory); | ||
| 648 | - } else { | ||
| 649 | - writer->json_keyvalue("available_memory", free_memory); | ||
| 650 | - } | ||
| 644 | + uint64_t available_memory = uv_get_available_memory(); | ||
| 645 | + writer->json_keyvalue("available_memory", available_memory); | ||
| 651 | 646 | ||
| 652 | 647 | if (uv_getrusage(&rusage) == 0) { | |
| 653 | 648 | double user_cpu = | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const availableMemory = process.availableMemory(); | ||
| 5 | + assert(typeof availableMemory, 'number'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments