Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Jonas 1 · Feb 16, 2011 at 11:22 AM · performanceoptimizationmemorygarbage-collection

Optimize (Garbage Collection, Memory usage)

I have some questions about Optimization.

I've heard that garbage collection will be executed automatically when developing for Windows in Unity. Is there any way to manually collect garbage in order to not affect game performance ?

Second, is there any performance or memory usage differences between those 2 code scripts ?

Code Script 1:

#pragma strict

private static var constNumber : float = 0.5f ;

function Update() { var fltCount: float = 0.7 + time.deltaTime fltCount -= constNumber }

Code Script 2

#pragma strict

private static var constNumber : float = 0.5f ; private var fltcount: float ;

function Update() { fltCount = 0.7 + time.deltaTime fltCount -= constNumber //could be written in one line but that's not my essential question }

Third question: When using the yield instructions, will there be a other CPU core used ? (If available)

Comment
Add comment
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

1 Reply

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

Answer by Statement · Feb 16, 2011 at 11:34 AM

1. You can probably use GC.Collect to manually tell garbage collector to clean out old data, although I haven't done manually so with Unity/Mono.

You also might want to look for pointers about when to use it.

2. Negligible difference, if any at all. I wouldn't worry. You can probably remove one arithmetic operation if you do:

fltCount = (0.7f - constNumber) + time.deltaTime; 

Because the first two values on right side are constant and can be computed compile time (Parenthesis added for clarity).

The compiler should output:

fltCount = 0.2f + time.deltaTime; 

EDIT: Actually, your constNumber is a static, and the compiler won't be able to make this assumption!

3. Yield instructions don't run threaded, so no. You must create your own threads for concurrent/parallel execution. When using threads to utilize several cores you must take action to avoid state corruption, deadlocks, and not call most of the Unity functions as they are generally not thread safe.

Comment
Add comment · Show 6 · 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 Jonas 1 · Feb 16, 2011 at 11:52 AM 0
Share

Thank you for your help. So declaring the variables outside the function ins$$anonymous$$d of inside the function doesn't affect performance ?

avatar image Statement · Feb 16, 2011 at 11:55 AM 1
Share

If you are curious about it, you can easily benchmark this yourself (and is a great way of understanding performance!). You can do a for loop that does this operation 100k times or something like that, for both scenarios and compare the results. Note that having a member variable adds to your complexity (ease of use) and increases your memory consumption since it has to tag along another float. I wouldn't worry, and I wouldn't use the member variable for this task. The time spent in your operation is magnitudes lesser than just the process of calling Update :)

avatar image Statement · Feb 16, 2011 at 11:57 AM 0
Share

Sometimes, the loop itself take up most of the computational time, so one way to help this would be to unroll the loop a bit, so you repeat the code several times within the loop. For the temporary variable, you can embed it inside a scope to avoid having to redefine the variable with different names, using { code; code; }. This would probably produce a fairer result.

avatar image Jonas 1 · Feb 16, 2011 at 11:58 AM 1
Share

Will try that. Thank you.

avatar image Statement · Feb 16, 2011 at 12:03 PM 0
Share

Actually, your constNumber isn't "const". The variable name fooled me :) It's "static", which doesn't hold true that the compiler will generate in-place computation. In either case, this is such a trivial example and your code will run just fine either case.

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

No one has followed this question yet.

Related Questions

Reasonable heap alloc. per second and total ? 0 Answers

UI-based game memory and performance issue. 1 Answer

ManagedHeap.ReservedUnusedSize 0 Answers

Display numbers in UI.Text Garbage Free? 2 Answers

Should GetComponent() generate garbage when the component is not found? 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