s1na
s1na

Reputation: 113

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.

Upvotes: 1

Views: 3224

Answers (1)

zzzz
zzzz

Reputation: 91253

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.

Upvotes: 7

Related Questions