這裏的第二篇文章,這真的讓我撓了撓頭。我有一個處理數組的函數來試圖找到類似的數據。該數組包含1410個元素,我認爲它很多,但Node或我的計算機不應該能夠處理的東西。Node.js - Segfault:11與相當大的陣列
我的代碼給出了「Segmentation Fault:11」錯誤,我發現它是與內存訪問問題有關的,所以我甚至希望儘可能測試我的Mac的RAM,但一切正常。 segfault使調試非常困難,這就是我來到這裏的原因。
那裏事情錯的代碼中的位置:
return matchings.map(matchArray => {
const namesList = matchArray
.map(matchItem => matchItem.name)
.sort((a, b) => a.localeCompare(b))
const symbolsList = matchArray
.map(matchItem => matchItem.symbol)
.sort((a, b) => a.localeCompare(b))
return {
name: common.getMode(namesList),
symbol: common.getMode(symbolsList),
matches: matchArray
}
}).sort((a, b) => a.name.localeCompare(b.name))
哪裏matchings
是我說的數組。 common.getMode(array)
有這樣的代碼:
array.sort()
const stats = {
top: {
name: '',
freq: 0
},
current: {
name: array[0],
freq: 1
}
}
for (let idxName = 1; idxName < array.length; idxName++) {
const currentName = array[idxName]
const lastName = array[idxName - 1]
if (currentName === lastName) {
stats.current.freq++
} else {
if (stats.current.freq > stats.top.freq) {
stats.top.name = stats.current.name
stats.top.freq = stats.current.freq
}
stats.current = {
name: currentName,
freq: 1
}
}
}
if (stats.current.freq > stats.top.freq) {
stats.top.name = stats.current.name
stats.top.freq = stats.current.freq
}
return stats.top.name
值得一提的是,與小尺寸〜1000的數組進行時,代碼工作正常,這使我相信這不是我的代碼。網上關於Node的Segfault 11的內容也很少,這沒有幫助。
任何想法非常感謝!
感謝您的回覆。非工作陣列的JSON在這裏:[鏈接](https://pastebin.com/SnVJM7xN),工作在這裏:[link](https://pastebin.com/GUYMMs6S)。有沒有什麼辦法可能是因爲等待承諾超時,因爲該函數需要大約8秒纔會發生段錯誤? –