我的golang程序(url监视器)有内存泄漏,它最终被内核(oom)杀死.环境:
$ go version
go version go1.0.3
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/data/apps/go"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CGO_ENABLED="1"
Run Code Online (Sandbox Code Playgroud)
码:
package main
import (
"bytes"
"database/sql"
"flag"
"fmt"
_ "github.com/Go-SQL-Driver/MySQL"
"ijinshan.com/cfg"
"log"
"net"
"net/http"
"net/smtp"
"os"
"strconv"
"strings"
"sync"
"time"
)
var (
Log *log.Logger
Conf cfg.KVConfig
Debug bool
CpuCore int
HttpTransport = &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(30 * time.Second)
c, err := net.DialTimeout(netw, addr, 20*time.Second)
if …Run Code Online (Sandbox Code Playgroud) 我正在使用OPROFILE来收集一些性能数据.但我陷入困境.
这是我的shell:
~ # rm -f /root/.oprofile/daemonrc
~ # opcontrol --setup --no-vmlinux
~ # opcontrol --init
~ # opcontrol --reset
~ # opcontrol --start
~ # opcontrol --status
Daemon running: pid 14909
Separate options: none
vmlinux file: none
Image filter: none
Call-graph depth: 0
~ # opcontrol --shutdown
Stopping profiling.
Killing daemon.
~ # opreport
error: no sample files found: profile specification too strict?
~ # tree /var/lib/oprofile/
/var/lib/oprofile/
??? abi
??? complete_dump
??? jitdump
??? opd_pipe
??? samples
??? …Run Code Online (Sandbox Code Playgroud) 我在Go中编写了一个URL监控程序,但经过一段时间后,我发现了很多ESTABLISHED条目netstat -nao|grep 80.
getHttpStatusCode函数:
HttpClient = &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(30 * time.Second)
c, err := net.DialTimeout(netw, addr, 20*time.Second)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
c.SetReadDeadline(deadline)
c.SetWriteDeadline(deadline)
return c, nil
},
},
}
// ...
func getHttpStatusCode(url string) int {
if url == "" {
return 200
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0
}
req.Close = true
req.Header.Add("User-Agent", …Run Code Online (Sandbox Code Playgroud)