我在4Gb机器上的64位Linux操作系统中运行以下代码:
package main
import (
"fmt"
"math"
)
func main() {
r := make([]bool, math.MaxInt32)
fmt.Println("Size: ", len(r))
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到:
Size: 2147483647
Run Code Online (Sandbox Code Playgroud)
如果我改变math.MaxInt32了math.MaxUint32我得到:
fatal error: runtime: out of memory
Run Code Online (Sandbox Code Playgroud)
随着切片大小math.MaxUint32我的内存不足,我期待那样,但当我尝试使用时,math.MaxInt64我得到:
panic: runtime error: makeslice: len out of range
Run Code Online (Sandbox Code Playgroud)
所以显然我无法创建一个大小为的切片math.MaxInt64,这让我们想到了一个问题:如果内存不是问题,那么我在Go中创建的最大切片是什么?
我记得在Java中,原始数组索引是用类型管理的int,所以原始数组的最大大小是a的最大值int,如果你尝试用long它来引发异常(据我记得)和Go一样吗?Go中的切片索引绑定到一个特定类型?
编辑:
我使用struct{}而不是bool分配math.MaxInt64元素来运行测试.一切都按预期进行,并打印:
Size: 9223372036854775807
Run Code Online (Sandbox Code Playgroud)
那么,另一个问题是,为什么当错误看起来相同(内存不足)时会出现两个不同的错误消息?
弹出每个错误的条件是什么?
Not*_*fer 14
根据文件,The elements can be addressed by integer indices 0 through len(s)-1.这意味着切片的最大容量是目标构建上的默认整数的大小.
编辑:从查看源代码,似乎有一个安全检查,以确保切片的大小是可能的:
func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct {
// NOTE: The len > MaxMem/elemsize check here is not strictly necessary,
// but it produces a 'len out of range' error instead of a 'cap out of range' error
// when someone does make([]T, bignumber). 'cap out of range' is true too,
// but since the cap is only being supplied implicitly, saying len is clearer.
// See issue 4085.
len := int(len64)
if len64 < 0 || int64(len) != len64 || t.elem.size > 0 && uintptr(len) > maxmem/uintptr(t.elem.size) {
panic(errorString("makeslice: len out of range"))
}
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下,它看起来像uintptr(len) > maxmem/uintptr(t.elem.size)我们不允许这样大小的分配.
但是,当我分配struct{}不占用内存时,允许此大小:
func main(){
r := make([]struct{}, math.MaxInt64)
fmt.Println(len(r))
}
// prints 9223372036854775807
Run Code Online (Sandbox Code Playgroud)