Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:14:12+00:00 2026-06-17T11:14:12+00:00

According to this Go Data Structures article, under the Strings section it states that

  • 0

According to this Go Data Structures article, under the Strings section it states that taking a slice of a string will keep the original string in memory.

“(As an aside, there is a well-known gotcha in Java and other languages that when you slice a string to save a small piece, the reference to the original keeps the entire original string in memory even though only a small amount is still needed. Go has this gotcha too. The alternative, which we tried and rejected, is to make string slicing so expensive—an allocation and a copy—that most programs avoid it.)”

So if we have a very long string:

s := "Some very long string..."

And we take a small slice:

newS := s[5:9]

The original s will not be released until we also release newS. Considering this, what is the proper approach to take if we need to keep newS long term, but release s for garbage collection?

I thought maybe this:

newS := string([]byte(s[5:9]))

But I wasn’t certain if that would actually work, or if there’s a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:14:14+00:00Added an answer on June 17, 2026 at 11:14 am

    Yes, converting to a slice of bytes will create a copy of the string, so the original one is not referenced anymore, and can be GCed somewhere down the line.

    As a “proof” of this (well, it proves that the slice of bytes doesn’t share the same underlying data as the original string):

    http://play.golang.org/p/pwGrlETibj

    Edit: and proof that the slice of bytes only has the necessary length and capacity (in other words, it doesn’t have a capacity equal to that of the original string):

    http://play.golang.org/p/3pwZtCgtWv

    Edit2: And you can clearly see what happens with the memory profiling. In reuseString(), the memory used is very stable. In copyString(), it grows fast, showing the copies of the string done by the []byte conversion.

    http://play.golang.org/p/kDRjePCkXq

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According this article , generic JPA DAO(Data Access Object) is a pretty nice pattern.
According to this paper differentiation works on data structures. According to this answer :
according to this image, i try to save data in NSUserDefaults but my App
According to this article from msdn ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496(v=vs.85).aspx ) the struct varies depending on
I set the data-main for Requirejs and according to the documentation that should set
According to this article poll vs select vs event-based : select() only uses (at
I have a url http://percipi.org/announce?sno=1&lim=1000 . According this data structure I want to display
I have the following data structure for a movies SQLite db (i will keep
I want to use uiwait according to this example code for sharing data between
I didnt find anything according this issue. Can jaas be used to secure my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.