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
1
Question by Trepan · Aug 26, 2011 at 04:48 AM · destroyfunctionprofiler

Why is the function "Destroy" taking so much time

I'm seeing a vast amount of time disappearing in the function "Destroy" when profiling my game on an iPad1 (it's also a factor on an iPad2 grab but orders of magnitude less painful). At the point I was recording I was doing a lot of procedural mesh creation and so destroying last frame's mesh each update. Most likely it's related to that?

Just wondering if anyone can shed any light on what exactly could be going on, i.e what could be being destroyed that would take so long (and why)? Most importantly is there anything I can do to eliminate or lessen the expense?

Hopefully you can see a screen-grab from the profiler below: alt text

Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Trepan · Aug 26, 2011 at 06:57 AM

Ok, so I've reworked my procedural mesh stuff to re-use the same mesh rather than calling Mesh.Destroy and creating a new one each time and that seems to have brought the "Destroy" time down considerably. I guess the take-away lesson: destroying stuff is slow, avoid.

Comment
Add comment · Show 5 · 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 Waz · Aug 26, 2011 at 07:19 AM 0
Share

Certainly it would be unusual in any game to be creating or Destroying a GameObject or a Component every frame.

avatar image Joshua · Aug 26, 2011 at 07:40 AM 0
Share

Be sure to watch this video: http://www.youtube.com/watch?v=IX041ZvgQ$$anonymous$$E even if you don't use C# you can use the object pooling script he writes, which is pretty awesome.

avatar image Trepan · Aug 26, 2011 at 04:20 PM 0
Share

Thanks for the comments - yes, I'm very familiar with pooling algorithms. In this case the mesh I'm rebuilding is a special 'one per frame' kind of deal whose parameters can vary wildly. $$anonymous$$ost likely I will refactor to improve my reuse of existing assets because no, I don't feel at all comfortable with a destroy/create cycle, but perhaps that leads me to a follow up question...

I'm from a long console development background and I'm very used to handling memory allocation super carefully - allocating multiple small objects in a single block, reusing memory, placement new, etc. Unfortunately all of those techniques seem to be hard, if not impossible to apply in the managed Unity environment. Are there any tricks that can allow you to do things like allocating a big array of vectors that you can manage yourself, i.e. passing on subsets of that array to other (child) objects? So far I've been just 'going with it' and letting GC do its thing, but this is against the grain, and cases like the one that prompted this thread add to my unease. As a specific challenge: when building a mesh procedurally, and when vert/tri counts will be changing dynamically, is it even possible to avoid reallocating at least the 'triangles' array (since the length of that array is the only way a mesh knows how many tris it contains)?!

avatar image Trepan · Aug 28, 2011 at 09:20 PM 0
Share

Perhaps the answer to reallocating the triangles array is to allocate a maximum sized array and then fill unused space with degenerate tris? ...I think I'll give that a go when I have a chance.

avatar image Waz · Aug 28, 2011 at 10:39 PM 0
Share

There isn't a lot you can do about arrays - Unity copies them every time they pass into or out of a setter/getter in the Unity API. Best you can do is $$anonymous$$imise other copies. $$anonymous$$aybe using arrays of same size will trigger optimisations inside Unity.

avatar image
0

Answer by jampoz · Aug 26, 2011 at 07:29 AM

That's called Object Pooling, isn't it?

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 Joshua · Aug 26, 2011 at 07:37 AM 0
Share

Yeah, exactly.

avatar image Waz · Aug 26, 2011 at 08:17 AM 0
Share

Actually, it's not. Object pooling is where you collect no-longer-used objects and reuse them next time one is needed. Trepan is doing better: reusing the object immediately, so not having the overhead of managing the pool.

avatar image Joshua · Aug 26, 2011 at 08:25 AM 0
Share

That's just a pool of one :p

avatar image Waz · Aug 26, 2011 at 08:42 AM 0
Share

I know you're joking, but it's exactly that sort of clean generic thinking that probably created the problem in the first place. There is a joke that whenever a programmer needs boiled water, he first empties the kettle in order to start from a known initial state.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy on touch 1 Answer

What is causing "Destroy" to take so much time when entering play mode? 1 Answer

Unity Profiler - Physics optimization (FPS drop) 0 Answers

Wy does this destroy script not work? 2 Answers

Purpose of the Destroy Function 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