关于Go切片截取的一个注意事项

go在使用切片截断时,底层数组没有改变,依然持有指向堆内存的指针,导致内存无法释放。

func split(arr []int) []int {
	newArr := arr[0:5]
	return newArr
}

以上切片newArr截取了切片arr中的部分数据并返回。如果切片arr在之后不再使用,即使切片newArr只使用了切片arr中的前5个数据而已,arr的整个底层数组都不会被GC回收。如果切片arr中含有大量的数据,一直不释放,那这会造成较大的浪费内存。

类似这种可以在截断前不需要的手动置空。

arr[5:] = nil
newArr := arr[0:5]

另外对于项目中Go代码情况可以pprof程序性能分析工具去查看。

This entry was posted in Go and tagged . Bookmark the permalink.

235 Responses

  1. Hey very cool website!! Man .. Excellent .. Amazing .. I’ll bookmark your web site and take the feeds also?KI’m glad to seek out a lot of useful information right here in the put up, we’d like work out more strategies on this regard, thanks for sharing. . . . . .

  2. I’ve been exploring for a bit for any high quality articles or blog posts in this kind of area . Exploring in Yahoo I eventually stumbled upon this website. Studying this info So i’m satisfied to express that I have a very just right uncanny feeling I discovered exactly what I needed. I such a lot indisputably will make certain to do not forget this website and provides it a glance on a constant basis.

  3. Thanks , I’ve recently been searching for information approximately this subject for a while and yours is the greatest I have found out so far. But, what in regards to the bottom line? Are you positive about the supply?

  4. Hello! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results. If you know of any please share. Thanks!

  5. I’m truly enjoying the design and layout of your website. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Fantastic work!

  6. Thanks for every other informative site. The place else may I am getting that kind of info written in such an ideal means? I have a mission that I am just now running on, and I’ve been on the glance out for such info.

  7. Hi there, just changed into aware of your weblog through Google, and found that it is really informative. I’m going to be careful for brussels. I’ll be grateful in the event you proceed this in future. Many folks might be benefited out of your writing. Cheers!

  8. I am not sure where you’re getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for wonderful info I was looking for this info for my mission.|

  9. I’m writing on this topic these days, safetoto, but I have stopped writing because there is no reference material. Then I accidentally found your article. I can refer to a variety of materials, so I think the work I was preparing will work! Thank you for your efforts.

  10. I am now not certain the place you are getting your information, but great topic.
    I needs to spend some time studying much more or working out more.
    Thank you for wonderful information I was searching for
    this information for my mission.

  11. Hello! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results. If you know of any please share. Cheers!

  12. Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

  13. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more clear from this post. I am very glad to see such great info being shared freely out there.

  14. Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

  15. Excellent blog you have here but I was wanting to know
    if you knew of any discussion boards that cover
    the same topics talked about in this article? I’d
    really love to be a part of online community where I
    can get responses from other knowledgeable people that share the same interest.
    If you have any suggestions, please let me know. Thanks!

  16. I’m amazed, I have to admit. Seldom do I encounter a blog that’s equally educative and engaging, and without a doubt, you have hit the
    nail on the head. The issue is something which
    not enough folks are speaking intelligently about. I’m
    very happy I came across this during my hunt for something regarding this.

  17. We are a gaggle of volunteers and starting a brand new scheme in our community.
    Your web site provided us with valuable info to work
    on. You have done a formidable process and our entire neighborhood will be thankful to you.

  18. Hello! This is kind of off topic but I need some guidance from
    an established blog. Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things out
    pretty quick. I’m thinking about making my own but I’m
    not sure where to start. Do you have any points or suggestions?
    Thanks

  19. Woah! I’m really loving the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s very
    difficult to get that “perfect balance” between superb usability and visual appearance.
    I must say you have done a superb job with this. Additionally,
    the blog loads extremely fast for me on Safari. Outstanding
    Blog!

  20. Fantastic goods from you, man. I have understand your stuff previous to and
    you’re just too great. I really like what you have
    acquired here, certainly like what you are stating and the way in which you
    say it. You make it entertaining and you still take care of to keep it sensible.
    I can not wait to read far more from you. This is actually
    a tremendous website.

  21. Thank you for the good writeup. It in fact was
    once a amusement account it. Look complex to more added agreeable from you!

    By the way, how could we keep up a correspondence?

  22. I was recommended this blog by my cousin.
    I am not sure whether this post is written by him as nobody else know such detailed about
    my difficulty. You’re wonderful! Thanks!

  23. I’m curious to find out what blog platform you are utilizing?
    I’m having some minor security problems with my latest blog and I’d like to find something more safe.
    Do you have any suggestions?

  24. I’m really inspired along with your writing talents as smartly as with the format in your
    weblog. Is that this a paid subject or did you customize it
    your self? Either way keep up the excellent quality writing, it’s uncommon to see a nice
    weblog like this one nowadays..

  25. Hello I am so glad I found your blog page, I really found you by error, while I was browsing on Bing for
    something else, Regardless I am here now and would
    just like to say cheers for a marvelous post and a all round enjoyable
    blog (I also love the theme/design), I don’t have time to
    read through it all at the moment but I have saved it and also included your RSS
    feeds, so when I have time I will be back to read more, Please do keep up the awesome job.

发表评论

邮箱地址不会被公开。 必填项已用*标注