導航:首頁 > 編程語言 > curlfc代碼

curlfc代碼

發布時間:2025-01-06 15:16:19

⑴ 怎麼自己檢查NodeJS的代碼是否存在內存泄漏

首先,我們來看一個簡單的內存泄漏
var http = require('http');

var server = http.createServer(function (req, res) {
for (var i=0; i<1000; i++) {
server.on('request', function leakyfunc() {});
}

res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
server.setMaxListeners(0);
console.log('Server running at http://127.0.0.1:1337/. Process PID: ', process.pid);

每一個請求我們增加了1000個導致泄漏的監聽器。如果我們在一個shell控制台中執行以下命令:
while true; do curl "http://127.0.0.1:1337/"; done

然後在另外一個shell控制台中查看我們的進程
top -pid

我們會看到node進程產生異常高的內存佔用,我們的node進程看起來失控了。那麼,當我們的node進程出現這種情況的時候,通常我們該怎樣診斷出問題的根源?
內存泄露的檢測
npm模塊 memwatch 是一個非常好的內存泄漏檢查工具,讓我們先將這個模塊安裝到我們的app中去,執行以下命令:
npm install --save memwatch

然後,在我們的代碼中,添加:
var memwatch = require('memwatch');
//memwatch.setup(); 原文有這行代碼,最新版本的memwatch已去掉這個方法(譯者注)

然後監聽 leak 事件
memwatch.on('leak', function(info) {
console.error('Memory leak detected: ', info);
});

這樣當我們執行我們的測試代碼,我們會看到下面的信息:
{
start: Fri Jan 02 2015 10:38:49 GMT+0000 (GMT),
end: Fri Jan 02 2015 10:38:50 GMT+0000 (GMT),
growth: 7620560,
reason: 'heap growth over 5 consecutive GCs (1s) - -2147483648 bytes/hr'
}

參考文獻https://w3ctech.com/topic/842#rd?sukey=0e1af5eeb690c5521ad1e164fb

閱讀全文

與curlfc代碼相關的資料

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接