英文:
How to empty out an arrayBuffer?
问题
buf
仍然存在于内存中,占用了大量的RAM(我猜与blob本身一样多)。我该如何清空它,以便可以释放内存?
英文:
I have a sample code as such :
async function example() {
const blob = new Blob(['bobbyhadz.com']);
const buf = await blob.arrayBuffer();
console.log(buf.byteLength);
}
example();
However buf
remains in memory and occupies the ram to a great extent ( as much as the blob itself I suppose ). How can I empty it out so I can get the memory back?
答案1
得分: 0
尝试这个,使用"let"代替"const"。
async function example() {
let blob = new Blob(['bobbyhadz.com']);
let buf = await blob.arrayBuffer();
buf = [];
console.log(buf.byteLength);
}
example();
英文:
try this one.use let instead off const
async function example() {
let blob = new Blob(['bobbyhadz.com']);
let buf = await blob.arrayBuffer();
buf = [];
console.log(buf.byteLength);
}
example();
答案2
得分: 0
以下是翻译好的代码部分:
发现这是一个仅在Ubuntu上出现的垃圾回收问题。我通过手动清理来解决它,如下所示:
const startCleaning = () => {
const cleanUpTimer = setInterval(() => {
if (global.gc) {
const checkIt = ~~(process.memoryUsage().arrayBuffers / 1024);
console.log('INTERVAL ACTIVE', checkIt);
if (checkIt > 5000) {
console.log('CLEANED!', checkIt);
global.gc();
} else {
console.log('INTERVAL DEACTIVATED!', checkIt);
clearInterval(cleanUpTimer);
}
}
}, 5000);
}
英文:
It turns out it is a garbage collection problem that only occurs on Ubuntu. I tackled it by manually cleaning it up as such :
const startCleaning = ()=> {
const cleanUpTimer = setInterval(()=>{
if(global.gc){
const checkIt = ~~(process.memoryUsage().arrayBuffers / 1024)
console.log('INTERVAL ACTIVE' , checkIt)
if(checkIt > 5000){
console.log('CLEANED!' , checkIt)
global.gc()
}else{
console.log('INTERVAL DEACTIVATED!' , checkIt )
clearInterval(cleanUpTimer)
}
}
},5000)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论