简体   繁体   中英

High memory usage for a simple Go program

The simplest question in Codechef is reading from input and writing to output as long as the number isn't 42. I wrote the following code:

package main

import "fmt"

func main() {
        var num int8
        fmt.Scanln(&num)
        for ; num != 42; fmt.Scanln(&num) {
                fmt.Println(num)
        }
}

It is accepted, though uses 124.6M memory according to the site. I wrote basically the same thing in C and it took 1.6M, I'm confused. Do you know what may have caused this?

I'm new to Go. It may be a bold mistake.

I didn't check, but I doubt your program uses 124+ MB of memory. I don't know where you got this number from, but I guess you're confusing allocated virtual memory and "used memory". Those two figures may be close to each other or not.

Go reserves a large memory area through the OS, but it's not "used memory" until actually further allocated by the Go runtime. Unclaimed virtual memory costs no real memory on most systems so is essentially free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM