- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我的内存有问题。我不明白为什么当我的程序长时间运行时 Go 使用越来越多的内存(从不释放它)。
第一次分配后,程序使用了将近 9 MB 的内存。然后在 12 小时后,它开始以指数方式使用更多内存,直到 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)
}))
//Call Func JsonReply(): return the finale Json response
str := JsonReply(fpath, JsonStruct_S.Options.Animated, JsonStruct_S.Options.Responsive, JsonStruct_S.Options.Clickurl, JsonStruct_S.Options.Width, JsonStruct_S.Options.Height, adid, campaignid, JsonStruct_S.Type, JsonStruct_S.Options.Aspectratio, mobile)
w.Header().Set("Content-Type", "application/json")
//writes FinaleJson to 'w' http.ResponseWriter(it contains the link of the second endpoint "/api/html/")
fmt.Fprint(w, str)
fmt.Println("successfull Operation !!")
fmt.Println("")
job_1.Complete(health.Success)
return true, nil
} else {
return false, nil
}
对于每个调用,我的服务需要使用我收到的参数生成一个新模板,如您所见,我为每个调用创建一个新端点,我不知道这是否是个好主意,我认为问题来了来自这部分代码,但我不确定,因为我不知道 GO 是如何管理它的。
最佳答案
显然,您不应该在每次出现请求时都创建处理程序。他们从不释放内存,所以你最终会遇到内存不足的异常。
相反,将处理程序端点放入数组( slice )并使用一个处理程序来响应请求,方法是查看此 slice 中的 URL,然后从不再需要的 slice 中删除该项目。
所以基本上,而不是
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, ...})
它会起作用 because :
Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash). Longer patterns take precedence over shorter ones, so that if there are handlers registered for both "/images/" and "/images/thumbnails/", the latter handler will be called for paths beginning "/images/thumbnails/" and the former will receive requests for any other paths in the "/images/" subtree.
当然,不要忘记清理数组 jobs
。
关于memory - 戈朗 : trouble with memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31720810/
我想知道将任何数据发送到模板(html/template 包)的真正方法是什么?我的代码如下: var templates = template.Must(template.ParseFiles(
我有密码 if config.Process.TraceLog != "" { f, err := os.OpenFile(config.Process.TraceLog, os.O_RDWR
大家好。我是 GO 的新手,需要一些帮助。我有一个具有这种结构的项目 但让我们假设我的逻辑包 中有 100500 个项目。如果我只需要为特定包导入一个或两个文件怎么办?我可以这样做还是只能导入完整包?
package main import ( "fmt" ) func main() { var square int box := [4]int{1, -2, 3, 4}
我想在每个范围循环后递增一个变量。但是,似乎无法使用标准(for init; condition; post {})语法,因此我想知道为什么。这是我正在尝试做的事情 for item := r
这个问题在这里已经有了答案: Creating a Constant Type and Restricting the Type's Values (2 个答案) 关闭 6 年前。 什么是只允许一组
我试过编译我的简单程序: func main(){fmt.Printf("Hello")} 根据文档在我的 PC 上使用 64 位 Debian Linux 到 MIPS 架构 https://gol
为什么不在 channel 上发送并阻止执行?我怎样才能使这个星座工作,以便我可以向 MoneyDive() 发送信号并继续执行? package main import ( "fmt" )
这是来自 Golang.org http://golang.org/pkg/sort/ // By is the type of a "less" function that defines the
class type User { name string } 是否可以在运行时添加字段age? 最佳答案 不,这是不可能的。 即使使用 Java 或 C++ 语法代替有效的 Go 也不行。
当我编译下面的程序时 func myPrint(v ...interface{}) { fmt.Println("Hello", v...) } func main() { m
有一个问题,这里发生了什么? forever := make(chan bool) log.Printf(" [*] Waiting for messages. To exit press CTRL+
当我运行我的 GoLang 程序时,它抛出了这个异常: packages/go-lang/1.3/pkg/tool/linux_amd64/6l: readsym 不同步 它是否来 self 的 am
我时常会遇到诸如零垃圾或有效使用内存等概念。例如在知名包的特性部分 httprouter您可以看到以下内容: Zero Garbage: The matching and dispatching pr
最近读了golang规范,遇到了一些有趣的操作符: & bitwise AND integers | bitwise OR integers
有这个包https://github.com/open-telemetry/opentelemetry-proto其中仅包含 protobuf 定义。要生成 golang 代码,必须输入: make
有这个包https://github.com/open-telemetry/opentelemetry-proto其中仅包含 protobuf 定义。要生成 golang 代码,必须输入: make
我正在使用下面的代码连接到 grpc 服务器,clientConn 对象用于所有后续的 rpc 调用。 maxDelay 设置为 5 秒。现在由于服务器出现问题,它没有响应 grpc 调用。所以我的客
初始数据: rawdata := []int{17, 23, 100500} 结果: result := convert(rawdata) expected := "1723100500" 我应该用
我想用我的智能手机操作我的窗帘。现在每次我改变手机的位置时都有一个方法 func main() { OnUpdate(func(tPos int) { wc(tPos,cPos)
我是一名优秀的程序员,十分优秀!