首页 > 文章列表 > 如何优化我的go fyne应用程序以保持稳定的内存使用量?

如何优化我的go fyne应用程序以保持稳定的内存使用量?

299 2024-02-28
问题内容

应用程序在运行时会将日志内容输出到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 随着时间的推移总是会变慢。