第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

golang:內(nèi)存問題

golang:內(nèi)存問題

Go
有只小跳蛙 2021-10-25 18:22:36
我記憶力有問題。我不明白為什么當(dāng)我的程序運(yùn)行很長時(shí)間時(shí) Go 使用越來越多的內(nèi)存(從不釋放它)。第一次分配后,程序使用了近 9 MB 的內(nèi)存。然后 12 小時(shí)后它開始以指數(shù)方式使用更多內(nèi)存,直到 800 MB。//.....code.....if bol {    // Assignment Struct.Var    Struct_VastScript.TxtNoticeTop = JsonStruct_S.Options.TxtNoticeTop    Struct_VastScript.TxtNoticeBottom = JsonStruct_S.Options.TxtNoticeBottom    Struct_VastScript.Loop = JsonStruct_S.Options.Loop    Struct_Image, err := getImage(Struct_VastScript.Video)    if err == nil {        if mobile == "true" {            Struct_VastScript.Image = Struct_Image.URL360        }    }    //open and parse a template file    fi = path.Join("templates/VastPlayer", "TempVastPlayer.txt")    tmpl, err := template.ParseFiles(fi)    if err != nil {        job_1.Complete(health.Panic)        return false, err    }    //substitute fields in the template 'tmpl', with values from 'XmlStruct_V' and write it out to 'buf'    var buf bytes.Buffer    if err := tmpl.Execute(&buf, Struct_VastScript); err != nil {        //if  err := tmpl.Execute(w, XmlStruct_V); err != nil {        job_1.Complete(health.Panic)        return false, err    }    // Call Func randString() : return alphanum random    dir := randString(12)    fpath := "http://creative2.xxx.io/api/html/" + dir    // Create a new EndPoint to write the generated 'template' on 'w' http.ResponseWriter    routeHtml := "/api/html/" + dir    http.HandleFunc(routeHtml, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        //writes Template to 'w' http.ResponseWriter        fmt.Fprintf(w, buf.String())        fmt.Println("successfull Operation 2 !!")        fmt.Println("")        job_2.Complete(health.Success)    }))對于每次調(diào)用,我的服務(wù)需要使用我收到的參數(shù)生成一個(gè)新模板,如您所見,我為每個(gè)調(diào)用創(chuàng)建了一個(gè)新端點(diǎn),我不知道這是否是個(gè)好主意,我認(rèn)為問題出在這部分代碼,但我不確定,因?yàn)槲也恢?GO 如何管理它。
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

顯然,您不應(yīng)該每次出現(xiàn)請求時(shí)都創(chuàng)建處理程序。他們從不釋放內(nèi)存,所以你最終會遇到內(nèi)存不足的異常。


相反,將處理程序端點(diǎn)放入數(shù)組(切片)并使用 ONE 處理程序,該處理程序通過查看此切片中的 URL 來響應(yīng)請求,然后從切片中刪除不再需要的項(xiàng)目。


所以基本上,而不是


routeHtml := "/api/html/" + dir

http.HandleFunc(routeHtml, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

    //writes Template to 'w' http.ResponseWriter

    fmt.Fprintf(w, buf.String())

    fmt.Println("successfull Operation 2 !!")

    fmt.Println("")

    job_2.Complete(health.Success)

}))


type JobInfo struct {

    Path string

    // some data here

}


// maybe global context

var jobs []JobInfo


// initialisation

jobs = make([]JobInfo, 0)


http.HandleFunc("/api/html/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

    path := r.URL.Path

    var job *JobInfo

    for _, j := range jobs {

        if j.Path == path {

            job = &j

            break

        }

    }


    if job != nil {

        // handle job request here

    }

}))


// and then in the jobs' loop

handlers = append(handlers, JobInfo{"/api/html/" + dir, ...})

它會起作用,因?yàn)椋?/p>


模式命名固定的根路徑,如“/favicon.ico”,或根子樹,如“/images/”(注意尾部斜杠)。較長的模式優(yōu)先于較短的模式,因此如果“/images/”和“/images/thumbnails/”都注冊了處理程序,則將為以“/images/thumbnails/”和前者開頭的路徑調(diào)用后一個(gè)處理程序?qū)⒔邮諏Α?images/”子樹中任何其他路徑的請求。


jobs當(dāng)然,不要忘記清理數(shù)組。


查看完整回答
反對 回復(fù) 2021-10-25
?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

而不是使用切片最好使用地圖


type JobInfo struct {

    Path string

    // some data here

}


    // global context

    var jobs map[string]JobInfo


// initialisation

jobs = make(map[string]JobInfoStruct)



http.HandleFunc("/api/html/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

    path := r.URL.Path

    var job JobInfoStruct

    var ok bool


    job, ok = jobs[path]

    if ok {

      // handle job request here

      //then after delete the job

      delete(jobs, path)

    }


}))


 // and then in the jobs' loop

pathVideo := "/api/html/" + dir

jobs[pathVideo] = JobInfoStruct{pathVideo, ...}


查看完整回答
反對 回復(fù) 2021-10-25
  • 2 回答
  • 0 關(guān)注
  • 286 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號