如何清空一个arrayBuffer?

huangapple go评论63阅读模式
英文:

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)}

huangapple
  • 本文由 发表于 2023年6月22日 13:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定