• 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 PrimeDerektive · Feb 01, 2012 at 01:32 AM · instantiatedestroypoolinggarbage-collectionpool

Pooling object performs the same or worse than instantiate/destroy?

So I recently started pooling and recycling the objects I frequently instantiate, because I've heard multiple times both here and on the forums that it is better for performance. However, when I make a build with an FPS counter, the performance during heavy instantiation sequences is pretty much the same, or sometimes worse, than simply instantiating/destroying. Is this normal, or is the benefit of instantiating going to be more of a long run kind of thing as the garbage heap starts to fill up?

I wrote my object pooling script myself, mostly with inspiration from this example, except in UnityScript and with some minor tweaks: http://snipt.org/ylxo2 I don't really see anything wrong with it, but maybe a fresh set of eyes might see something.

Comment
Add comment · Show 6
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 syclamoth · Feb 01, 2012 at 01:39 AM 0
Share

Well, for starters I wouldn't be using Unity's 'Array' class. It's terribly slow.

avatar image PrimeDerektive · Feb 01, 2012 at 02:14 AM 0
Share

Oops, I had the wrong link in there to the script I got my inspiration from, I updated it. I used Array because I knew it was for the most part the same performance wise as ArrayList, but I just noticed that script was using generic lists. Do you think that would improve a bit?

avatar image syclamoth · Feb 01, 2012 at 02:30 AM 0
Share

Well, I personally can't really help you with UnityScript optimisation. I don't really know much about that. I could help more if it were C#, I know that.

avatar image Eric5h5 · Feb 01, 2012 at 02:39 AM 2
Share

Never use Array or ArrayList, Array is a good 30X slower than built-in arrays, and 10X slower than List (in general). Any reasonably-implemented pooling will always outperform instantiate/destroy. I don't see what the point is of your container object; there's no reason to parent pooled objects to anything.

avatar image PrimeDerektive · Feb 01, 2012 at 03:14 AM 0
Share

Thanks Eric... List it is, then. The container is only to organize my scene view hierarchy when I play in the editor, more of an OCD thing.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by PhobicGunner · Dec 22, 2012 at 12:38 AM

The purpose of object pooling is not to instantiate objects faster, since it most certainly will NOT be faster. BUT, what an object pool does is LIMIT MEMORY CONSUMPTION. Think about it this way - normally you Instantiate and Destroy object. When you Instantiate an object, it allocates memory. When you Destroy an object, it will eventually get cleaned up by Garbage Collection. If you have a lot of objects being instantiated and destroyed frequently, it will almost certainly start triggering the GC which can result in frequent hiccups and freezes.

However, in an object pooling system, the object is instantiated, but never actually destroyed. Rather, it is de-activated and then later when another object of the same type needs to be instantiated, instead of instantiating it just re-activates the previous object. In many cases (particularly frequent calls to Instantiate) this can save tons of memory, and in the long run eliminate a lot of garbage collection.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

How do you use a pool of objects rather than creating and destroying repeatedly? 8 Answers

Problem with object pooling and reseting the properties of a pooled object 0 Answers

Instantiate and Destroy optimization 1 Answer

General pooling question 1 Answer

Firing only a single projectile at a time, then adding a second. 2 Answers


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