D
D
Dmitry2022-01-14 23:47:30
go
Dmitry, 2022-01-14 23:47:30

Why does a goroutine weigh so little?

The specification of the current version says that the initial size of the goroutine is 2kb.
At the same time, they are based on system threads that are at least 1MB in size.

Why is a single goroutine program only 2kb based on the system thread?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2022-01-14
@R3cive

At the same time, they are based on system threads that are at least 1MB in size.

This is the main misconception. Goroutines are not based on system threads, system threads execute goroutines. A goroutine is a "task", a thread is a "executor". The executor can execute the task until it is completed or until the executor has something to do (rather than waiting for a network or disk, for example). After that, the performer looks for a new task. To do this, he has his own task queue. If there is something in it, this "something" (another goroutine) is executed. If not, the executor goes to the "global queue" and removes a few tasks from there (more than one). If there is nothing in that queue, it goes to "steal" (yes, it's literally called stealing) tasks from the queues of other executors (neighboring threads).
So, a stream is far from equal to a goroutine. The thread has a system stack,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question