Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mun-Yeong-Seok · Jul 08, 2017 at 07:42 AM · optimizationstring

String garbage optimization question.

It's a small part, but I wonder.

  1. It is a simple game that starts with game start -> game play -> game end. Then, for each play, initialize some string values to specific values. ex) str1 = "Hello1"; str2 ="Hello2"; In this case, is it better to declare the const class field separately and allocate the value rather than creating and assigning the string immediately? ex) private const strHello = "Hello"; ... str1 = strHello;

  2. There is the following score code. scoreText.text = score.ToString("0.00") + 'm'; If the score is increased from 0.0 to 100.0 in 0.1 unit to update the text, will 1000 garbage occur?

  3. I know that StringBuilder is used to cause less string garbage. But does StringBuilder.Append() be used for even two strings? Or do you have your own criteria(3 or more, 4 or more ...) to use StringBuilder.Append()?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Jul 08, 2017 at 09:01 AM 0
Share

Honestly strings don't create that much garbage. You shouldn't worry about it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jul 08, 2017 at 09:40 AM

To point 1:

No, string literals in your code are "interned". That means those strings are already constants. If you assign the same string to two variables they actually reference the exact same string.

 string str1 = "Hello";
 string str2 = "Hello";

In this case str1 and str2 will reference the same string object instance.

To point 2:

First score.ToString("0.00") will allocate a new string with at least 4 character. Then when executing +'m' a new string with at least 5 characters will be allocated. The 4 character string will be immediately up for garbage collection. When the text variable is replaced by a new text the old one is also up for garbage collection. So yes, if you execute that line 1000 times you will allocate two string instances each time, one with at least 4 and one with at least 5 characters.

To point 3:

You can use a StringBuilder (as long as you cache and re-use your stringbuilder). However simply using string.Format might be simpler:

 scoreText.text = string.Format("{0:0.00}m", score);

Of course this would still allocate at least a 5 character string each time it's called. The format string never changes since it's an interned string literal.

Strings are immutable objects. So without some unsafe-hacks the value of a string can't be changed once created. That means when you want to change the content of a string you have to allocate a new string. @vexe wrote a garbage-free string wrapper(gstring) which could be used to reduce garbage allocation. However it might be tricky to use, has limited string feature support and most likely is not threadsafe.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mun-Yeong-Seok · Jul 08, 2017 at 10:08 AM 0
Share

'Cache' means to store a StringBuilder in a variable and use it? And the format string can not be changed means that if a string is updated, it will inevitably be allocated memory?

avatar image Bunny83 Mun-Yeong-Seok · Jul 08, 2017 at 10:37 AM 0
Share

Right, but even when you cache the StringBuilder each string you "create" will need to be allocated as actual string.

A StringBuilder basically just uses a List<char> internally. When you use any Append method you actually add chars to that internal list. As long as the lists capacity is not reached nothing will be allocated internally. However to finally get a string from the StringBuilder you would use the ToString method of the StringBuilder. This will create a new string, however with the final appropriate length. So you allocate the least memory possible.

avatar image Mun-Yeong-Seok Bunny83 · Jul 08, 2017 at 10:57 AM 0
Share

Thanks for the detailed explanation! link text It is like this. Clear() and Append()

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Display numbers in UI.Text Garbage Free? 2 Answers

Optimizing OnGUI 1 Answer

Will caching strings improve performance for animation? (and other string calls) 1 Answer

Is there something like Animator.StringToHash() but for tags? 0 Answers

Cheap options for making diffuse-lit objects "interesting" 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges