Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

扩展不定期崩溃, 内存溢出 #71

Closed
hhuimie opened this issue May 26, 2022 · 14 comments
Closed

扩展不定期崩溃, 内存溢出 #71

hhuimie opened this issue May 26, 2022 · 14 comments
Labels
question Further information is requested
Milestone

Comments

@hhuimie
Copy link

hhuimie commented May 26, 2022

浏览器版本: Edge 101.0.1210.32 (正式版本) (64 位)
Scriptcat版本: 0.9.1
脚本数量3-5个, 可能每小时运行, 数据量大概几万条, 有些脚本会做成爬虫, 基本几天会崩溃一次
但是之前用油猴是没这问题的

@CodFrm
Copy link
Member

CodFrm commented May 26, 2022

方便发一下脚本看看么?

@CodFrm
Copy link
Member

CodFrm commented May 26, 2022

image

这个页面有些什么报错没?

@hhuimie
Copy link
Author

hhuimie commented May 27, 2022

没看到什么特别的报错, 不过我在一个新的Edge浏览器, 只安装了scriptcat和油猴(版本4.16.1), 同时测试了一下, 脚本如下

Scriptcat脚本

// ==UserScript==
// @name         New Userscript
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @require      https://cdn.jsdelivr.net/npm/scriptcat-lib@1.1.1/dist/gm.js
// @definition   https://cdn.jsdelivr.net/npm/scriptcat-lib@1.1.1/src/types/gm.d.ts
// @grant        GM_xmlhttpRequest
// @grant        GM_log
// @grant        GM_setValue
// @grant        GM_getValue
// @crontab      */1 * * * *
// @connect      https://s1.ax1x.com
// ==/UserScript==

let done;
gm.ajax({method:'get',url:'https://s1.ax1x.com/2022/05/26/XEW4te.png'})
    .then(resp=>{
        let count = GM_getValue('count',0);
        GM_log(count++);
        GM_setValue('count',count);
        GM_log(JSON.stringify(resp).substr(0,1000));
        console.log(resp);
        done();
    }).catch(e=>{
        let count = GM_getValue('count',0);
        GM_log(count++);
        GM_setValue('count',count);
        done();
    });
return new Promise((resolve, reject) => {
    // Your code here...
    done = resolve;
});

油猴脚本

// ==UserScript==
// @name         New Userscript
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @include      https://www.baidu.com/
// ==/UserScript==

setInterval(function(){
    GM_xmlhttpRequest({
        method:'get',
        url:'https://s1.ax1x.com/2022/05/26/XEW4te.png',
        onload:function(resp){
            let count = GM_getValue('count',0);
            console.log(count++);
            GM_setValue('count',count);
            console.log(JSON.stringify(resp).substr(0,1000));
            console.log(resp);
        }
    });
},60*1000);

结果如下:
scriptcat运行了167次后崩溃, 显示内存溢出
image

油猴运行到200+都没事

@hhuimie
Copy link
Author

hhuimie commented Jun 9, 2022

请问还有什么需要我提供的吗?
目前还是会崩溃
3CCF88E1

@CodFrm
Copy link
Member

CodFrm commented Jun 9, 2022

请问还有什么需要我提供的吗? 目前还是会崩溃 3CCF88E1

暂时没了吧,还得去调试,近期可能修复不了,在准备一次大的重构,应该能解决这个问题。可能和现在的消息机制也有关系,之前开发的时候确实也没考虑内存泄漏的问题

@hhuimie
Copy link
Author

hhuimie commented Jun 21, 2022

扩展不稳定, 真是挺难受的.
不知道能不能先用一些其他野路子解决这个问题呢?
现在我是直接用ahk每天重启一遍edge.....
https://stackoverflow.com/questions/13104360/how-to-restart-a-chrome-extension-automatically

@CodFrm
Copy link
Member

CodFrm commented Jun 21, 2022

想不到了,是必须用到后台脚本嘛?不是可以用暴力猴那些替代一下

@CodFrm CodFrm added this to the v0.10.x milestone Nov 2, 2022
@CodFrm CodFrm added the question Further information is requested label Nov 2, 2022
@CodFrm
Copy link
Member

CodFrm commented Nov 2, 2022

不清楚op还关注这个么?今天测试了一下,代码中有将日志打到控制台 console.log(resp); 这个也会导致内存过大,我删除这一行后,使用下面的代码测试,发现确实是有内存泄漏问题(只是说没那么严重了),当停用脚本时内存恢复正常,这个问题会在最新版本去尝试修复

// ==UserScript==
// @name         New Userscript
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @require      https://cdn.jsdelivr.net/npm/scriptcat-lib@1.1.1/dist/gm.js
// @definition   https://cdn.jsdelivr.net/npm/scriptcat-lib@1.1.1/src/types/gm.d.ts
// @grant        GM_xmlhttpRequest
// @grant        GM_log
// @grant        GM_setValue
// @grant        GM_getValue
// @crontab      */5 * * * * *
// @connect      https://s1.ax1x.com
// ==/UserScript==

let done;
gm.ajax({method:'get',url:'https://s1.ax1x.com/2022/05/26/XEW4te.png'})
    .then(resp=>{
        let count = GM_getValue('count',0);
        GM_log(count++);
        GM_setValue('count',count);
        GM_log(JSON.stringify(resp).substr(0,1000));
        // console.log(resp);
        done();
    }).catch(e=>{
        let count = GM_getValue('count',0);
        GM_log(count++);
        GM_setValue('count',count);
        done();
    });
return new Promise((resolve, reject) => {
    // Your code here...
    done = resolve;
});

@CodFrm
Copy link
Member

CodFrm commented Nov 9, 2022

image

image

image

执行若干次后,内存波动正常

不过内存占用挺高的,后续继续优化

@CodFrm CodFrm closed this as completed Nov 9, 2022
@hhuimie
Copy link
Author

hhuimie commented Nov 17, 2022

image image image

执行若干次后,内存波动正常

不过内存占用挺高的,后续继续优化

测试了下0.10.x版本, 比0.9.x的稳定, 但是依然会内存溢出
image

@CodFrm
Copy link
Member

CodFrm commented Nov 17, 2022

可以给一下脚本,或者执行频率么?

@hhuimie
Copy link
Author

hhuimie commented Nov 18, 2022

可以给一下脚本,或者执行频率么?

脚本不太方便给, 好多个脚本, 10分钟左右运行一次, 会下载, 处理xlsx/json, 然后上传
我用上面简单的脚本貌似也没法快速复现问题, 我也有点无奈
6点开始运行的脚本, 到现在3g了
image

@CodFrm
Copy link
Member

CodFrm commented Nov 18, 2022

有类似的脚本么?可以摘取一些关键代码,没有的话,比较难判断是不是脚本的内存泄漏问题

上述脚本,脚本5s会执行一次,上次测试80次,没有出现泄漏问题

因为后台脚本是持续的在一个页面中运行的,如果不对资源主动的释放是会很容易产生这个问题,我听哥哥的描述,脚本业务还是挺复杂的

@CodFrm
Copy link
Member

CodFrm commented Dec 1, 2022

image

上述脚本执行600次后,内存情况

image

我觉得大概率还是哥哥的代码问题,写后台脚本的时候确实要注意这内存释放的问题

哥哥也可以去油猴中文网贴一下相关代码或者关键代码https://bbs.tampermonkey.net.cn/,社区还是挺活跃的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants