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 /
This question was closed Oct 20, 2013 at 09:47 AM by meat5000 for the following reason:

Satisfied

avatar image
0
Question by meat5000 · Oct 17, 2013 at 01:11 PM · instantiateperformancedestroycollectiongarbage

Instantiate/Destroy Garbage Collection advice

I instantiate a gameobject (300verts) every 3secs or less and they get destroyed either 'at random' (wrt time) or in batches on 20, from start to finish.

Should I run Garbage Collection through the course of the game or allow auto GC to do its work?


The case :

I run the following code

 function pullIn()
 {
     var hitColliders = Physics.OverlapSphere(Vector3(0,0,0), detectRadius, allBlockLMask);
         
         for (var i = 0; i < hitColliders.Length; i++)
         {
             var myTransform : Transform = hitColliders[i].transform;
             
             if ( myTransform.rigidbody.isKinematic == true )
             {
                 myTransform.rigidbody.isKinematic = false;
                 myTransform.rigidbody.MovePosition(myTransform.rigidbody.position + myTransform.TransformDirection(Vector3.down*blockMoveAmount));
                 //myTransform.rigidbody.isKinematic = true; // <- (1)
             }
         }
 }

After which I feel a little lagginess for the rest of the game. (The routine works perfectly with no lag - not the issue).

Adding line (1) (marked in the code) solves the problem so I figure the lag is down to constant physics response in my objects, however this doesnt seem to be a problem before the routine.

Should I run Garbage Collection through the course of the game or allow auto GC to do its work? Will it help in this particular case or will the benefits be negligible?

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

  • Sort: 
avatar image
3
Best Answer

Answer by whydoidoit · Oct 17, 2013 at 03:02 PM

I wouldn't GC during the game - can you not do something about pooling those objects?

Reason being, the version of Mono we are saddled with has only one generation and a GC is going to look at absolutely everything (which is not what happens in the current version of Mono or in the Microsoft versions). The only answer is to allocate a bunch of memory (then deallocate it) and hope you get away with it, or pool everything you can :S

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 meat5000 ♦ · Oct 17, 2013 at 03:27 PM 0
Share

$$anonymous$$y game generally is a great performer so I find myself getting away with a lot. It is happy to instantiate ~450 gameobjects before it stutters at all. I guess it's why I never got round to implementing a pool.

If GC was run in a coroutine, would the impact still be noticable in the main?

I suppose the real question should be, after Unity destroys an object, what residue is left?

avatar image whydoidoit · Oct 17, 2013 at 03:49 PM 1
Share

Yes it will be impacting the game, because a coroutine isn't a thread and in any case GC has to pause all running threads.

The problem of residue is every byte it ever used will need to be collected...

avatar image meat5000 ♦ · Oct 17, 2013 at 04:11 PM 0
Share

GC has to pause all running threads

Wow that's proper heavy.

Thanks dude!

avatar image meat5000 ♦ · Oct 17, 2013 at 11:39 PM 0
Share

So, I tried it anyway, just to see the effects, for completeness :)

I make the call right at the end of my above function and there is absolutely zero impact. Does this mean there is no garbage?

avatar image whydoidoit · Oct 17, 2013 at 11:42 PM 1
Share

$$anonymous$$aybe, but perhaps just nothing free yet. If you call Destroy it isn't gone until the end of the current frame.

Show more comments

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Instantiate vs caching GameObject in scene 3 Answers

Replace GameObject vs. replacing mesh and material? 2 Answers

Accessing value from dict allocates 28 bytes 0 Answers

instatiate gameobject after 1 second 1 Answer

Resources.load loads twice ( onTriggerenter->load-another->destroy-self ) 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