登录
首页 >  Golang >  Go问答

如何解决我的go fyne应用程序前台刷新时内存不断增加的问题?

来源:stackoverflow

时间:2024-02-07 22:57:23 160浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《如何解决我的go fyne应用程序前台刷新时内存不断增加的问题?》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

应用程序在运行时会将日志内容输出到multilineentry,应用程序在前台时占用的内存会逐渐增加,但在后台却能保持稳定的占用。我想知道什么问题是?

这是应用程序的主要功能

var conf = &conf{}

func main() {
    go http.listenandserve("0.0.0.0:6060", nil)
    a := app.new()
    a.settings().settheme(&mytheme{})
    w := a.newwindow("forward")

    tab := container.newapptabs(
        container.newtabitem("data1", weatherscreen()),
        container.newtabitem("data2", tossscreen()),
        //container.newtabitem("per", preferencescreen(w)),
        container.newtabitem("about", widget.newlabel("version:1.0")),
    )
    //tab.settablocation(container.tablocationleading)
    rootvbox := container.newvbox(tab, createlogs())

    w.setcontent(rootvbox)

    w.resize(fyne.newsize(700, 364))
    w.setmaster()
    w.setfixedsize(true)
    resource, _ := fyne.loadresourcefrompath("graphicloads-polygon-next-2.ico")
    a.seticon(resource)
    if desk, ok := a.(desktop.app); ok {
        m := fyne.newmenu("forward",
            fyne.newmenuitem("show", func() {
                w.show()
            },
            ),
        )
        //desk.setsystemtrayicon(resource)
        desk.setsystemtraymenu(m)
    }
    w.seticon(resource)
    w.setcloseintercept(func() {
        //hide
        w.hide()
    })
    w.centeronscreen()
    initdata()
    //longip := float32(80)
    //shortport := float32(50)
    conf = loadconfig()
    w.show()
    a.run()

}

这是我用来处理日志的函数

var logflag = make(chan string, 10)
var cour = 1
var text = binding.newstring()
func createlogs() fyne.canvasobject {
    logs := widget.newentrywithdata(text)
    logs.multiline = true
    logs.validator = nil
    logs.resize(fyne.newsize(688, 230))
        //update text
    go updatelogs(logflag, logs)
    contain := container.newwithoutlayout(logs)
    return contain
}
func updatelogs(l chan string, logs *widget.entry) {
    for {
        if d, ok := <-l; ok {
            t, _ := text.get()
            text.set(t + d)
            logs.cursorrow = cour
            runtime.gc()
        }
    }
}
func appendlogs(name string, format string, a ...any) {
    now := time.now()
    year, month, day := now.date()
    str := fmt.sprintf(format, a...)

    y := strconv.itoa(year)
    var m string
    if int(month) >= 10 {
        m = strconv.itoa(int(month))
    } else {
        m = "0" + strconv.itoa(int(month))
    }
    var d string
    if day >= 10 {
        d = strconv.itoa(day)
    } else {
        d = "0" + strconv.itoa(day)
    }

    dateheader := y + "/" + m + "/" + d + " "

    hour, min, sec := now.clock()
    var h string
    if hour >= 10 {
        h = strconv.itoa(hour)
    } else {
        h = "0" + strconv.itoa(hour)
    }
    var mi string
    if min >= 10 {
        mi = strconv.itoa(min)
    } else {
        mi = "0" + strconv.itoa(min)
    }
    var s string
    if sec >= 10 {
        s = strconv.itoa(sec)
    } else {
        s = "0" + strconv.itoa(sec)
    }
    timeheader := h + ":" + mi + ":" + s + " "
    header := dateheader + timeheader + name + " "
    str = header + str + "\n"
    cour++
    if cour > 50 {
        runtime.gc()
        cour = 1
        text.set("")
    }
    logflag <- str

}

每次有日志导出时都会调用一次appendlogs。

我尝试限制日志条目数并清除绑定数据的内容,但这并没有阻止内存增长。

if cour > 50 {
        runtime.GC()
        cour = 1
        text.Set("")
    }

我也尝试注释掉整个appendlogs,但这也没有解决问题。

我希望当应用程序位于前台时内存保持稳定。


正确答案


Entry 添加文本行会导致性能问题,因为它当前呈现所有内容,而且 SetText 必须解析所有换行符以进行布局。

为了更好地处理大数据,您可以考虑使用 List 每行带有 Label,不进行解析,也不在屏幕外渲染项目。

未来版本的 Entry 旨在提高内存使用率,但由于解析要求,单个 SetText 随着时间的推移总是会变慢。

到这里,我们也就讲完了《如何解决我的go fyne应用程序前台刷新时内存不断增加的问题?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>