ZhangZhihui's Blog  

 

 

 

 

As a rule of thumb, remember that slicing a large slice or array can lead to potential high memory consumption. The remaining space won’t be reclaimed by the GC, and we can keep a large backing array despite using only a few elements. Using a slice copy is the solution to prevent such a case.

 

We have seen that slicing can cause a leak because of the slice capacity. But what about the elements, which are still part of the backing array but outside the length range? Does the GC collect them?

 

posted on 2024-02-15 20:04  ZhangZhihuiAAA  阅读(9)  评论(0编辑  收藏  举报