在循环中调用promise时如何防止内存泄漏

时间:2019-10-13 14:30:31

标签: nativescript

我似乎出错了

  

ActivityManager:进程nl.xxxx.yyyy(pid 21526)具有   死亡:最前面(2411,292)ActivityManager:setHasOverlayUi被调用   未知的pid:21526

当我循环调用下面提到的函数时。在大约200次通话中,出现错误。看来这不是一个计时问题,因为即使我在3秒钟的超时(调用pomise之后)中调用该函数,它也会在大约200个周期后死亡。我尝试将所有变量放在函数之外,将变量设置为null,但似乎无济于事。我已经用手指钩着骨头,但是什么也没出现……有人知道我需要做什么吗?

function testrun(f)
{
        // initially called with f = 0
        if(f > 500) return; // limit test to 500 cycles
        console.log ("fire :"+ f); // show the cycle you're in
        getMyThumb("/storage/emulated/0/DCIM/Screenshots/Screenshot_20190710-092009_ScanApp.jpg")
        .then( thumb =>
        {
            console.log(thumb);
            setTimeout(() => 
            {
                testrun(f+1);
            }, 5000);   // tried setting timeout from 100ms to 5 seconds per cycle... All bugg out at ca. 200 cycles            
        })
        global.gc(); // testd with\ without  garbage collection     
}


global.getMyThumb = function name(filepath) 
{
    return new Promise((resolve, reject)=>
    {
       global.gc(); // tried with \ without   garbage collection here 
       imageSource = imageSourceModule.fromFile(filepath);
        try
        {
            var mutable = BitmapFactory.makeMutable(imageSource); 
            var ThumbBitmap = BitmapFactory.asBitmap(mutable).dispose((bmp) =>
            {   
                var optisizestring = "25,25";
                test =  bmp.resize(optisizestring); 
                base64JPEG = test.toBase64(BitmapFactory.OutputFormat.JPEG, 75);
                img = imageSource.fromBase64(base64JPEG);
                resolve( "data:image/png;base64," + base64JPEG);                
                global.gc(); // tried with \ without   garbage collection here
             });
        } catch(ex) { console.log("errds " + ex); resolve (null);}
    });
}

0 个答案:

没有答案