2016-10-20 25 views
6

Bây giờ tôi sử dụng:Làm thế nào để phát hiện sử dụng v8 đống gần hạn chế trong Node.js

const v8 = require('v8'); 

let heap = v8.getHeapStatistics(); 
let usage = 100/heap.heap_size_limit * heap.used_heap_size; 

if (usage > 90) { 
    console.log(`V8 heap usage close to the limit (${usage.toFixed()}%)`); 
} else if (usage > 95) { 
    console.log(`V8 heap usage very close to the limit (${usage.toFixed()}%)`); 
} 

Giải pháp này không hoạt động đúng.

Khi tôi cố gắng lệnh này: nút max-cũ-gian-size = 100 index.js quá trình của tôi giảm cấp phát bộ nhớ không thể, khi kịch bản tính toán của tôi ~ 56%

getHeapStatistics().heap_size_limit is 178 MB 
getHeapStatistics().used_heap_size is ~95 MB 

Làm thế nào để phát hiện tình hình chính xác hơn, khi nào chúng ta có thể thoát khỏi lỗi bộ nhớ?

Trả lời

1

Vùng v8 thực sự được chia thành các vùng hoặc không gian: new_space, old_space, code_space, map_space và large_object_space. Hai chữ cái đầu tiên tương ứng với vườn ươm và đống đối tượng có người thuê, mặc dù trong các đối tượng lý thuyết có thể ngồi trong bất kỳ không gian nào trong năm không gian.

Một biện pháp chính xác hơn sẽ là sử dụng space_size và space_used_size của old_space.

var space = v8.getHeapSpaceStatistics(); 
var old_space_total = space[1].space_size; 
var old_space_used = space[1].space_used_size; 
var usage = 100/old_space_total * old_space_used; 
if (usage > 90) { 
    console.log(`V8 heap usage close to the limit (${usage.toFixed()}%)`); 
} else if (usage > 95) { 
    console.log(`V8 heap usage very close to the limit (${usage.toFixed()}%)`); 
} 

kê ngay trước khi kiệt sức nhớ:

#node --max_old_space_size=200 heap.js 
... 
{ total_heap_size: 243306496, 
    total_heap_size_executable: 5242880, 
    total_physical_size: 243306496, 
    total_available_size: 19149632, 
    used_heap_size: 215801616, 
    heap_size_limit: 243269632 } 
[ { space_name: 'new_space', 
    space_size: 33554432, 
    space_used_size: 16464888, 
    space_available_size: 41992, 
    physical_space_size: 33554432 }, 
    { space_name: 'old_space', 
    space_size: 205475840, 
    space_used_size: 197215464, 
    space_available_size: 18462512, 
    physical_space_size: 205475840 }, 
    { space_name: 'code_space', 
    space_size: 2097152, 
    space_used_size: 680416, 
    space_available_size: 617024, 
    physical_space_size: 2097152 }, 
    { space_name: 'map_space', 
    space_size: 2179072, 
    space_used_size: 1452528, 
    space_available_size: 18800, 
    physical_space_size: 2179072 }, 
    { space_name: 'large_object_space', 
    space_size: 0, 
    space_used_size: 0, 
    space_available_size: 0, 
    physical_space_size: 0 } ] 
V8 heap usage close to the limit (96%) 
... 
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 

Như chúng ta có thể thấy, total_heap_size: 243.306.496 thực sự là một tổng kết của không gian mới (33.554.432), không gian cũ (205.475.840), mã (2.097.152), bản đồ (2179072), đối tượng lớn (0).

Và như bạn có thể thấy, tất cả các không gian khác đều lành mạnh, trong khi không gian cũ gần cạn kiệt và sự cạn kiệt thực sự đến từ đó.

Hy vọng điều này sẽ hữu ích.

Các vấn đề liên quan