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 MathijsF · Jun 21, 2017 at 08:29 AM · unity 5windows-standalone

Hiccups, simple lines of code sometimes taking several milliseconds

So I've been looking at this for the past 2 days and I just can't figure it out.

The problem is that I'm getting FPS drops every second or every few seconds. They don't show up well in the profiler, so I resorted to adding Stopwatches to the code. I'm getting weird results, both in the Editor and in a Windows standalone build.

The code below is called from within a Unity coroutine, literally copy & pasted, no modifications:

 var stopwatch = new Stopwatch();
 stopwatch.Start();
 var go = new GameObject();
 var elapsed = stopwatch.ElapsedMilliseconds;
 if (elapsed > 2)
 {
     Debug.Log("new GameObject took " + elapsed + "ms");
 }

This actually triggers sometimes. Log shows "new GameObject took 4ms". And not only this statement, but other similiarly extremely simple statements that I surround with a Stopwatch. It seems random.

What it LOOKS like is that the main thread is somehow freezing or locking up. How is this possible? In a Unity coroutine, there shouldn't be ANYTHING else going on right? Garbage Collection, Physics, Rendering, other Coroutines etc. should be running either before or after?

And again it's not just 'new GameObject', other statements also take several milliseconds sometimes.

We've also tried disabled the Garbage Collector (see: https://forum.unity3d.com/threads/how-to-disable-the-garbage-collector-on-windows.369015/). Does not seem to make a difference, in both cases the weird freezes seem to happen.

Edit: extra information that could be relevant. We do create a TON of GameObjects, like in the order of 1000 per second. It's a big open world that uses a lot of procedurally generated models. Yes, we use tricks like manual batching to try and reduce the amount as much as possible, but we often need the actual individual gameobjects to exist independently.

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
0

Answer by FlaSh-G · Jun 21, 2017 at 10:05 AM

You assumptions are all correct as far as I can tell. But perhaps it's not accurate to run a stopwatch over a single line of code. I ran my Bencher over new GameObject() and got results between 3 and 3.8µs. So even though it is rightfully discouraged to prefer instantiating and destroying over proper object pooing, a "new GameObject" line every now and then shouldn't be the cause of your problem.

Comment
Add comment · Show 2 · 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 MathijsF · Jun 21, 2017 at 02:42 PM 0
Share

Thanks for your response. I feel I should mention, we do create a TON of GameObjects, like in the order of 1000 per second. It's a big open world application that uses a lot of procedurally generated models. Aside from discussing whether or not we should do this (yes, we batch to try and reduce the amount as much as possible, but we often need the actual individual gameobjects to exist independently), is this likely a limitation of Unity? A thousand GOs per second causes hidden problems under the hood?

avatar image FlaSh-G · Jun 21, 2017 at 03:19 PM 0
Share

I don't have too much hard data, but object pooling is generally advised. Be careful with SetActive though, it's not the most performant thing either. I'd personally be interested in implementing a pooling solution myself if I were in your situation, but there are also some polling solutions in the Asset Store.

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

127 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 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

how to decrease transparent of object by mouse click 0 Answers

Delete file in iOS with Application.dataPath get UnauthorizedAccessException: Access to path is denied 1 Answer

Camera not showing grass if looking down at 90 2 Answers

Unaccounted time between: Start of Frame and Camera.FindStacks 1 Answer

running Unity holoLens 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