My [GO] language program is getting bigger?

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The program is simple, with a simple HTTP request to protect the server daemon

Package Mainimport ("FMT" "Net" "Net/http" "Strings" "Time") var (info []net. Addrip Stringgeturl stringrequest *http. Requestresp *http. Responseclient *http. CLIENTIPX string) func main () {info, _ = net. Interfaceaddrs () IP = strings. Split (Info[0]. String (), "/") [0]FMT. PRINTLN (IP) fmt. PRINTLN ("Alarm would begin in the second..") Fmt. PRINTLN ("Please don't close ...") I: = 10for i > 0 {time. Sleep (time. Second * 1) fmt. Println (i) I--}geturl = "http:/" + IP + "/alarmbegin" request, _ = http. Newrequest ("GET", Geturl, nil)///not mustclient = &http. Client{}resp, _ = client. Do (Request) FMT. Println (RESP) fmt. Println ("Done") go Iva () Ivs ()}func ivsoffline () {//ivs Break processing 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 would restart in 1 min ") time. Sleep (time. Second *) 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 line processing func ivaoffline () {defer func () {//} must first declare defer, otherwise the panic exception cannot be caught 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)//Wait 10 minutes, the purpose is to wait for the IVA server recovery 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 (Ten * time. Second) for {select {case <-ivs. C:ivsoffline ()}}}func IVA () {IVA: = time. Newticker (Ten * time. Second) for {select {case <-iva. C:ivaoffline ()}}}

First, the runtime is added to the code. GC, garbage collection, invalid result, not a memory that doesn't appear to be released

Then try to debug.freeosmemory never release heap memory to the system, or is constantly getting bigger!!!


Later found to be a TCP connection,

Add in Code


If resp! = nil {resp. Body.close ()}

Close IO to resolve

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.