|
| 1 | +const v8 = require('v8'); |
| 2 | + |
| 3 | +// 从 V8 版本、命令行标志、以及检测到的 CPU 特性派生的版本标记 |
| 4 | +console.log(v8.cachedDataVersionTag()); |
| 5 | +// 4151506697 |
| 6 | + |
| 7 | +// 获取 v8 堆空间的统计信息 |
| 8 | +console.log(v8.getHeapSpaceStatistics()); |
| 9 | +// [ { space_name: 'read_only_space', |
| 10 | +// space_size: 524288, |
| 11 | +// space_used_size: 35200, |
| 12 | +// space_available_size: 480384, |
| 13 | +// physical_space_size: 43904 }, |
| 14 | +// { space_name: 'new_space', |
| 15 | +// space_size: 2097152, |
| 16 | +// space_used_size: 966784, |
| 17 | +// space_available_size: 64384, |
| 18 | +// physical_space_size: 2046080 }, |
| 19 | +// { space_name: 'old_space', |
| 20 | +// space_size: 2330624, |
| 21 | +// space_used_size: 2154208, |
| 22 | +// space_available_size: 144, |
| 23 | +// physical_space_size: 2197728 }, |
| 24 | +// { space_name: 'code_space', |
| 25 | +// space_size: 1048576, |
| 26 | +// space_used_size: 559968, |
| 27 | +// space_available_size: 0, |
| 28 | +// physical_space_size: 592736 }, |
| 29 | +// { space_name: 'map_space', |
| 30 | +// space_size: 536576, |
| 31 | +// space_used_size: 349976, |
| 32 | +// space_available_size: 0, |
| 33 | +// physical_space_size: 367384 }, |
| 34 | +// { space_name: 'large_object_space', |
| 35 | +// space_size: 0, |
| 36 | +// space_used_size: 0, |
| 37 | +// space_available_size: 1520180736, |
| 38 | +// physical_space_size: 0 } ] |
| 39 | + |
| 40 | + |
| 41 | +// 获取 v8 堆整体统计信息 |
| 42 | +console.log(v8.getHeapStatistics()); |
| 43 | +//{ total_heap_size: 7708672, |
| 44 | +// total_heap_size_executable: 3670016, |
| 45 | +// total_physical_size: 6046160, |
| 46 | +// total_available_size: 1492594096, |
| 47 | +// used_heap_size: 4353160, |
| 48 | +// heap_size_limit: 1501560832, |
| 49 | +// malloced_memory: 8192, |
| 50 | +// peak_malloced_memory: 1185552, |
| 51 | +// does_zap_garbage: 0 } |
| 52 | + |
| 53 | + |
| 54 | +// setFlagsFromString |
| 55 | +for (let i = 0; i < 100; i++) { |
| 56 | + const a = 1; |
| 57 | + setTimeout(() => console.log(a), 10); |
| 58 | +} |
| 59 | + |
| 60 | +v8.setFlagsFromString('--trace_gc'); |
| 61 | +setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 10*1000); |
| 62 | + |
0 commit comments