網站首頁 編程語言 正文
使用場景
一句話總結:保存和復用臨時對象,減少內存分配,降低GC壓力
sync.Pool
是可伸縮的,也是并發安全的,其大小僅受限于內存大小。sync.Pool
用于存儲那些被分配了但是沒有使用,而未來可能會使用的值。這樣就可以不用再次經過內存分配,可直接復用已有對象,減輕GC的壓力,從而提升系統性能。
使用方法
聲明對象池
type Student struct { Name string Age int32 Remark [1024]byte } func main() { var studentPool = sync.Pool{ New: func() interface{} { return new(Student) }, } }
Get & Put
type Student struct { Name string Age int32 Remark [1024]byte } var buf, _ = json.Marshal(Student{Name: "lxy", Age: 18}) func Unmarsh() { var studentPool = sync.Pool{ New: func() interface{} { return new(Student) }, } stu := studentPool.Get().(*Student) err := json.Unmarshal(buf, stu) if err != nil { return } studentPool.Put(stu) }
-
Get()
用于從對象池中獲取對象,因為返回值是interface{}
,因此需要類型轉換 -
Put()
則是在對象使用完畢之后,返回對象池
性能測試
以下是性能測試的代碼:
package benchmem import ( "encoding/json" "sync" "testing" ) type Student struct { Name string Age int32 Remark [1024]byte } var buf, _ = json.Marshal(Student{Name: "lxy", Age: 18}) var studentPool = sync.Pool{ New: func() interface{} { return new(Student) }, } func BenchmarkUnmarshal(b *testing.B) { for n := 0; n < b.N; n++ { stu := &Student{} json.Unmarshal(buf, stu) } } func BenchmarkUnmarshalWithPool(b *testing.B) { for n := 0; n < b.N; n++ { stu := studentPool.Get().(*Student) json.Unmarshal(buf, stu) studentPool.Put(stu) } }
輸入以下命令:
?go test -bench . -benchmem
以下是性能測試的結果:
goos: windows
goarch: amd64 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
pkg: ginTest ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
BenchmarkUnmarshal-8 ? ? ? ? ? ? ? 17004 ? ? ? ? ? ? 74103 ns/op ? ? ? ? ? ?1392 B/op ? ? ? ? ?8 allocs/op
BenchmarkUnmarshalWithPool-8 ? ? ? 17001 ? ? ? ? ? ? 71173 ns/op ? ? ? ? ? ? 240 B/op ? ? ? ? ?7 allocs/op
PASS
ok ? ? ?ginTest 3.923s
在這個例子中,因為 Student 結構體內存占用較小,內存分配幾乎不耗時間。而標準庫 json 反序列化時利用了反射,效率是比較低的,占據了大部分時間,因此兩種方式最終的執行時間幾乎沒什么變化。但是內存占用差了一個數量級,使用了 sync.Pool
后,內存占用僅為未使用的 240/1392 = 1/6
,對 GC 的影響就很大了。
我們甚至在fmt.Printf
的源碼里面也使用了sync.Pool
進行性能優化!
原文鏈接:https://blog.csdn.net/qq_61039408/article/details/128498046
相關推薦
- 2022-10-09 Xshell連接不上虛擬機的解決辦法匯總_Linux
- 2022-12-14 Android?MaterialAlertDialogBuilder修改按鈕屬性_Android
- 2022-11-18 詳解C語言內核字符串轉換方法_C 語言
- 2022-07-22 定時任務和延時任務詳解
- 2022-03-08 Docker自定義網絡詳細介紹_docker
- 2022-12-01 關于Linux之grep查找文本時匹配反斜杠\轉義問題_linux shell
- 2022-05-31 利用python庫matplotlib繪制不同的圖表_python
- 2023-08-13 常見的linux命令
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支