簡體   English   中英

NODE.JS:致命錯誤 - JS分配失敗 - 在解析大型json對象時處理內存不足

[英]NODE.JS: FATAL ERROR- JS Allocation failed - process out of memory, while parsing large json objects

我正在嘗試解析並向mongodb數據庫添加一些從API獲取的數據。 我希望從特定時間到今天為每個用戶獲取所有數據。 所以我正在做的是,我每次迭代檢索每個用戶數據5天,所以它就像2-3個月的數據分成5天。

出於某種原因,我收到了Allocation Failer的錯誤 - 處理內存不足。

似乎我在到達特定用戶時遇到此錯誤,因為他似乎比其他人擁有更多數據。

我在運行腳本時嘗試過這個命令:node --max-old-space-size = 4028 worksnap.js。

我的代碼看起來像這樣:

var currentMonth = new Date();
    var startDate = new Date("February 1, 2016 00:00:00");  //Start from February

var counter = 1;
while (startDate.getMonth() <= currentMonth.getMonth()) {
    //todo:: look if u have to increaze the start time, due the previous end time becomes start time it can take the same time time entries (have to be reviewd and make sure)....
    var from = new Date(startDate).getTime() / 1000;
    startDate.setDate(startDate.getDate() + 5);
    var to = new Date(startDate).getTime() / 1000;
    iterateThruAllStudents(from, to);
} 

function getTimeEntriesFromWorksnap(error, response, body) {
        //console.log(response.statusCode);
        if (!error && response.statusCode == 200) {
            parser.parseString(body, function (err, results) {
                var json_string = JSON.stringify(results.time_entries);
                var timeEntries = JSON.parse(json_string);
                _.forEach(timeEntries, function (timeEntry) {
                    _.forEach(timeEntry, function (item) {
                        saveTimeEntry(item);
                    });
                });
            });
        }
    }

    function saveTimeEntry(item) {
        Student.findOne({
                'worksnap.user.user_id': item.user_id[0]
            })
            .populate('user')
            .exec(function (err, student) {
                if (err) {
                    throw err;
                }
                student.timeEntries.push(item);
                student.save(function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log('item inserted...');
                    }
                });

            });
    }

    function iterateThruAllStudents(from, to) {
        Student.find({status: 'student'})
            .populate('user')
            .exec(function (err, students) {
                if (err) {
                    throw err;
                }

                _.forEach(students, function (student, i) {
                    if (student.worksnap.user != null) {
                        setTimeout(function () {
                            var options = {
                                url: 'https://api.worksnaps.com/api/projects/' + project_id + '/time_entries.xml?user_ids=' + student.worksnap.user.user_id + '&from_timestamp=' + from + '&to_timestamp=' + to,
                                headers: {
                                    'Authorization': 'Basic bGhNSVwJkVUFasSxx2loOFVyZkFyOENEZEsdxxxCdUlHdElWMHo0czo='
                                }
                            };
                            request(options, getTimeEntriesFromWorksnap);
                        }, 5000 * i);
                    }
                });
            });
    }

誰知道我在做錯了什么?

這是一個評論,因為它不包含解決方案。

有兩件事看起來很可疑

一個問題是:

while (startDate.getMonth() <= currentMonth.getMonth()) {
  //todo:: look if u have to increaze the start time, due the previous end time becomes start time it can take the same time time entries (have to be reviewd and make sure)....
  var from = new Date(startDate).getTime() / 1000;
  startDate.setDate(startDate.getDate() + 5);
  var to = new Date(startDate).getTime() / 1000;
  iterateThruAllStudents(from, to);
} 

您不必等到處理一名學生的數據,而是同時請求所有學生的數據。

類似的問題是setTimeout ,因為根據執行時間,您的代碼需要在內存中保存多個請求的數據。

您應該使用async或Promise之類的東西來解決異步循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM