我的[GO]语言程序变大了?

zjgdtl · · 1552 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

程序很简单,通过简单的http请求来守护服务器后台程序

package main

import (
	"fmt"
	"net"
	"net/http"
	"strings"
	"time"
)

var (
	info    []net.Addr
	ip      string
	geturl  string
	request *http.Request
	resp    *http.Response
	client  *http.Client
	ipx     string
)

func main() {

	info, _ = net.InterfaceAddrs()
	ip = strings.Split(info[0].String(), "/")[0]
	fmt.Println(ip)
	fmt.Println("alarm will begin in 30 second..")
	fmt.Println("please DO NOT close..")
	i := 10
	for i > 0 {
		time.Sleep(time.Second * 1)
		fmt.Println(i)
		i--
	}

	geturl = "http://" + ip + "/alarmbegin"
	request, _ = http.NewRequest("GET", geturl, nil)
	///not must
	client = &http.Client{}
	resp, _ = client.Do(request)
	fmt.Println(resp)
	fmt.Println("done")
	go Iva()
	Ivs()
}

func IvsOffLine() {
	//ivs断线处理

	info, _ = net.InterfaceAddrs()
	ipx = strings.Split(info[0].String(), "/")[0]
	//fmt.Println(ipx)
	if ipx == "0.0.0.0" {
		fmt.Println("network problem....")
		fmt.Println(strings.Split(info[0].String(), "/")[0])
		for strings.Split(info[0].String(), "/")[0] == "0.0.0.0" {
			time.Sleep(time.Second * 5)
			info, _ = net.InterfaceAddrs()
		}
		fmt.Println("network problem fixed.. alarm will restart in 1 min")
		time.Sleep(time.Second * 30)
		geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/refresh"
		request, _ = http.NewRequest("GET", geturl, nil)
		resp, _ = client.Do(request)
		fmt.Println(resp)
		time.Sleep(time.Second * 2)
		fmt.Println("alarm begin..")
		geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/alarmbegin"
		request, _ = http.NewRequest("GET", geturl, nil)
		resp, _ = client.Do(request)
		fmt.Println(resp)
		fmt.Println("done..")
	}

}

//iva 断线处理
func IvaOffLine() {

	defer func() { //必须要先声明defer,否则不能捕获到panic异常
		if err := recover(); err != nil {
			fmt.Println(err) //panic
		}
	}()
	geturl = "http://192.168.10.57/milsng/SVSProxy/"
	request, _ = http.NewRequest("GET", geturl, nil)
	resp, _ = client.Do(request)
	if resp == nil {
		fmt.Println("iva connect fail...try to reconnect")
		for resp == nil {
			time.Sleep(time.Second * 5)
			geturl = "http://192.168.10.57/milsng/SVSProxy/"
			request, _ = http.NewRequest("GET", geturl, nil)
			resp, _ = client.Do(request)
			fmt.Println("iva connect fail...try to reconnect")
		}
		fmt.Println("iva connect refreshing... pls wait")
		info, _ = net.InterfaceAddrs()
		if strings.Split(info[0].String(), "/")[0] != "0.0.0.0" {
			time.Sleep(time.Minute * 10) //等待10分钟,目的是等待iva服务器恢复
			geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/refresh"
			request, _ = http.NewRequest("GET", geturl, nil)
			resp, _ = client.Do(request)
			fmt.Println(resp)
			time.Sleep(time.Second * 2)
			fmt.Println("alarm begin..")
			geturl = "http://" + strings.Split(info[0].String(), "/")[0] + "/alarmbegin"
			request, _ := http.NewRequest("GET", geturl, nil)
			resp, _ = client.Do(request)
			fmt.Println(resp)

		}
	}


	//fmt.Println("iva connect success")
}

func Ivs() {
	ivs := time.NewTicker(10 * time.Second)
	for {
		select {
		case <-ivs.C:
			IvsOffLine()
		}
	}
}

func Iva() {
	iva := time.NewTicker(10 * time.Second)
	for {
		select {
		case <-iva.C:
			IvaOffLine()
		}
	}
}

首先 在代码中加入了runtime.GC, 回收垃圾, 结果无效,看来并不是未释放的内存

然后尝试了debug.freeosmemory  从来释放堆内存给系统 ,还是在不断变大!!!


后来发现是  TCP连接,

在代码中加入


	if resp != nil {
		resp.Body.Close()
	}

 关闭 io , 遂解决

版权声明:本文为博主原创文章,未经博主允许不得转载。


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:zjgdtl

查看原文:我的[GO]语言程序变大了?

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1552 次点击  
加入收藏 微博
1 回复  |  直到 2000-01-01 00:00:00
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传