Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High CPU usage #40249

Closed
santhoshkarthi opened this issue Jul 16, 2020 · 7 comments
Closed

High CPU usage #40249

santhoshkarthi opened this issue Jul 16, 2020 · 7 comments

Comments

@santhoshkarthi
Copy link

santhoshkarthi commented Jul 16, 2020

What version of Go are you using (go version)?

Go Version : 1.14.3

$ go version

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

Linux AMD64, as well as ARM

go env Output
$ go env

What did you do?

I have just created two go routine and just printing one statement with sleep of 5 secs, so both go routine were running concurrently but when I checked the CPU always it was taking more than 70% in imx6ul(ARMv7)

package main

import (
	"fmt"
	"time"
)

var a chan string

/*
func thread_1(a chan string) {
	for {
		//time.Sleep(time.Second * 2)
		fmt.Println("Sending...")
		a <- "Go lang"
		fmt.Println("Sender is done")
	}
}
*/
func thread_1() {
	for {
		//time.Sleep(time.Second * 2)
		fmt.Println("Sending...")
		a <- "Go lang"
		fmt.Println("Sender is done")
	}
}

func thread_2() {
	for {
		fmt.Println("Receiving...")
		time.Sleep(time.Second * 10)
		fmt.Println(<-a)
		time.Sleep(time.Second)
		fmt.Println("Receiver is done")
	}
}

func main() {
	a = make(chan string)
	fmt.Println("Entered into main")
	//	go thread_1(a)
	//	go thread_2(a)
	go thread_1()
	go thread_2()

	for {

	}

}

What did you expect to see?

It should not use much CPU

What did you see instead?

It was taking more CPU,

@DisposaBoy
Copy link

for {

}

This is what's called a busy loop. The only thing it does is burn CPU.

You should replace it with select {}

@ALTree
Copy link
Member

ALTree commented Jul 16, 2020

What @DisposaBoy said. It's the busy loop in the main function that is burning cpu cycles. Closing here, since this is not a Go bug.

@ALTree ALTree closed this as completed Jul 16, 2020
@santhoshkarthi
Copy link
Author

@ALTree Thanks for quickly replying, thanks alot, but I want to run the program indefinitely, This is what we use to do in "C", like while(TRUE);

@ALTree
Copy link
Member

ALTree commented Jul 16, 2020

The while 1 pattern burns cpu needlessly in C, too, and it's definitely a bad choice in C, too. Go is not different. But in Go you can use select { } to sleep indefinitely in main, as it was suggested above.

@santhoshkarthi
Copy link
Author

Thanks alot @ALTree as you said, I replaced with select{} now it looks good. Thanks again for your quick reply.
and one more thing, may i know why go program take more virtual memory, do we need to worry about that,
image

@santhoshkarthi
Copy link
Author

santhoshkarthi commented Jul 16, 2020

If you see "go_lang_prog" and "unix_server" are written in GO, and it is showing 779 MB of virtual memory, why it is taking such huge memory

@davecheney
Copy link
Contributor

@santhoshkarthi it looks like your original question has been answered. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@golang golang locked and limited conversation to collaborators Jul 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants