slice記憶體分配

2021-12-30 11:23:17 字數 3067 閱讀 5356

slice是golang提供的乙個很好的符合型別。既支援資料動態擴充套件,又能隨機訪問,使資料保持很好的區域性性。但是slice有乙個效能可能導致資料的一致性和預期不一致,就是它會按需為slice收集記憶體。參考這段**:

package main

import "fmt"

func main() ? ? ?//what's the differenct to assign underlying or not

var data2 = [...]string //what's the differenct to assign underlying or not

inttest := 3

fmt.printf("inttest %%t after append is %t \n", inttest)

fmt.printf("inttest %%p after append is %p \n", inttest)

fmt.printf("data %%v is %v \n", data)

fmt.printf("data %%q is %q \n", data)

out := data[:0]

for _, s := range data

}fmt.printf("data %%v after append is %v \n", data)

fmt.printf("data %%q after append is %q \n", data)

fmt.printf("data %%s after append is %s \n", data)

fmt.printf("data %%t after append is %t \n", data)

fmt.printf("data %%p after append is %p \n", data)

fmt.printf("&data %%p after append is %p \n", &data)

fmt.printf("data2 %%t after append is %t \n", data2)

fmt.printf("data2 %%p after append is %p \n", data2)

fmt.printf("&data2 %%p after append is %p \n", &data2)

fmt.printf("&data2 %%d after append is %d \n", &data2)

fmt.printf("out %%v after append is %v \n", out)

fmt.printf("out %%q after append is %q \n", out)

out2 := append(data[:0], "a", "b", "c", "d")

fmt.printf("data %%v after append is %v \n", data)

fmt.printf("data %%q after append is %q \n", data)

fmt.printf("out2 %%v after append is %v \n", out2)

fmt.printf("out2 %%q after append is %q \n", out2)

out3 := append(data[:0], "a", "b", "c")

fmt.printf("data %%v after append is %v \n", data)

fmt.printf("data %%q after append is %q \n", data)

fmt.printf("out3 %%v after append is %v \n", out3)

fmt.printf("out3 %%q after append is %q \n", out3)

}output:

inttest %t after append is intinttest %p after append is %!p(int=3)data %v is [one? three]data %q is ["one" "" "three"]data %v after append is [one three three]data %q after append is ["one" "three" "three"]data %s after append is [one three three]data %t after append is stringdata %p after append is 0xc420062150&data %p after append is 0xc42007a020data2 %t after append is [3]stringdata2 %p after append is %!p([3]string=[one? three])&data2 %p after append is 0xc420062180&data2 %d after append is &[%!d(string=one) %!d(string=) %!d(string=three)]out %v after append is [one three]out %q after append is ["one" "three"]data %v after append is [one three three]data %q after append is ["one" "three" "three"]out2 %v after append is [a b c d]out2 %q after append is ["a" "b" "c" "d"]data %v after append is [a b c]data %q after append is ["a" "b" "c"]out3 %v after append is [a b c]out3 %q after append is ["a" "b" "c"]

從輸出裡我們可以得出兩條結論:

1. 對比第17行(append資料長度超出capacity)和第21行的輸出(append資料長度未超出capacity),slice在容量足夠的時候修改已有的array,在容量不夠的時候才realloc記憶體基於新的記憶體建立array

2. 對比第12行和9行,golang是強型別的,%p只對引用內型或者取位址有效

3. 對比第9行和第10行,golang對slice和&slice的值定義了不同的語義

關於slice分配記憶體

slice是golang提供的乙個很好的符合型別。既支援資料動態擴充套件,又能隨機訪問,使資料保持很好的區域性性。但是slice有乙個效能可能導致資料的一致性和預期不一致,就是它會按需為slice收集記憶體。參考這段 output data v is one three data q is one ...

slice記憶體分析

切片是引用型別的資料 每乙個切片引用了底層的陣列 切片本身不儲存任何資料,都是這個底層的陣列儲存,所以修改切片也就是修改這個陣列中的資料 向切片中新增資料的時候,如果沒有超過容量,直接新增,如果超過容量自動增長 切片一旦擴容,就重新指向乙個新的底層陣列 package main import fmt...

記憶體分配 Go記憶體管理 記憶體分配一

go作為乙個比較新晚 新 的語言,自然借鑑前輩們的優點,比如說語言本身負責記憶體管理 對協程和高併發的高優支援 簡單高效的語法等。本篇及後續的幾篇要講的就是還沒提到的比較複雜的記憶體管理。學習記憶體管理 分配 前,如果有jvm的記憶體管理的基礎,會變得非常簡單,如果是第一次接觸記憶體管理,在看完go...