how about golang channel memory usage?

1,469 views
Skip to first unread message

Lynn H

unread,
Jan 15, 2018, 12:04:19 PM1/15/18
to golang-nuts
I user golang to write tcp  server recently, I'm puzzled by  memoy usage with channel

so I write some code for test
   
import (
    "time"
)

func main() {
    memsize := 256
    for i := 0; i < 100000; i++ {
        go func() {
            m := make(chan []byte, memsize)
            time.Sleep(30 * time.Second)
            <-m
        }()
    }
    time.Sleep(30 * time.Second)
}
 I user  RES value of 'top' command to get program memory use 
  memsize = 0,  program use 258M (memory just use to  allocate for 100000 routines)
  memsize = 256,program use 306M, 100000 channel use 48M
  memsize = 512, program use 328M,100000 channel use 70M
  memsize = 1024,program use 379M, 100000 channel use 121M
  memsize = 2048,program use 455M, 100000 channel use 197M

so what the rules of channel size and  memory usage?

Jan Mercl

unread,
Jan 15, 2018, 12:46:45 PM1/15/18
to Lynn H, golang-nuts



On Mon, Jan 15, 2018 at 6:04 PM Lynn H <yu3...@gmail.com> wrote:

> so what the rules of channel size and memory usage? 



--

-j

Lynn H

unread,
Jan 15, 2018, 10:49:12 PM1/15/18
to golang-nuts
not understand ur table,
when memsize is 256,single channel use  6114Byte memory?

在 2018年1月16日星期二 UTC+8上午1:46:45,Jan Mercl写道:

Jan Mercl

unread,
Jan 16, 2018, 1:23:08 AM1/16/18
to Lynn H, golang-nuts
On Tue, Jan 16, 2018 at 4:49 AM Lynn H <yu3...@gmail.com> wrote:

> not understand ur table,
> when memsize is 256,single channel use 6114Byte memory?

Size of []byte is 3 words, assuming a 64 bit system that's 24 bytes. 256*24 = 6,144 bytes ie. size of a chan []byte with capacity 256, which is what make(chan []byte, 256) produces.

In general, make(chan T, N) means reserve N*sizeof(T) bytes for the channel buffer.



--

-j

Lynn H

unread,
Jan 16, 2018, 1:52:05 AM1/16/18
to golang-nuts
OK, get it ,
you means virt memory use, I test it, result data could match the table

but how about res memory usage? how  RES memory used in golang? 

在 2018年1月16日星期二 UTC+8下午2:23:08,Jan Mercl写道:

Jan Mercl

unread,
Jan 16, 2018, 6:16:21 AM1/16/18
to Lynn H, golang-nuts
On Tue, Jan 16, 2018 at 7:52 AM Lynn H <yu3...@gmail.com> wrote:

> but how about res memory usage? how RES memory used in golang? 

Most process allocations typically do not care about the virtual memory being immediately mapped to physical RAM. The operating systems takes care about that usually only after a virtual memory page is touched for the first by the process. In any case, unless the process explicitly asks otherwise, the physical RAM allocation strategy is normally left for the OS to decide. I don't know enough details about the particular case of the Go runtime in this respect.


--

-j

Reply all
Reply to author
Forward
0 new messages