douda5227 2012-04-07 01:15
浏览 41
已采纳

如何在Go中分配16GB内存?

I'm using the following simple Go code to allocate a 3D array of size 1024x1024x1024:

grid = make([][][]TColor, 1024)
for x = 0; x < 1024; x++ {
    grid[x] = make([][]TColor, 1024)
    for y = 0; y < 1024; y++ {
        grid[x][y] = make([]TColor, 1024)
    }
}

That TColor struct is a 4-component float64 vector:

type TColor struct { R, G, B, A float64 }

Halfway (x=477 and y=~600ish) through the allocation, the inner-most make() call panics with... runtime: out of memory: cannot allocate 65536-byte block (17179869184 in use)

This works fine with lower grid resolutions, ie 256³, 128³ etc. Now since the size of the struct is 4x4 bytes, that whole grid should require exactly 16 GB of memory. My machine (openSuse 12.1 64bit) has 32 GB of addressable physical (ie not-virtual) memory. Why can Go (weekly.2012-02-22) not allocate even half of this?

  • 写回答

2条回答 默认 最新

  • dongyingla8668 2012-04-07 01:32
    关注

    The struct has 4x8 bytes, not 4x4.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献