Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 arielsan · Feb 10, 2014 at 05:11 PM · performancegetcomponentmemorygarbage-collectiongarbage

Should GetComponent() generate garbage when the component is not found?

We were profiling (deep profile) our game to check about cpu and garbage generation when we found out that GetComponent() was generating garbage in some cases. After making some tests we discovered that it was when the component is not found and GetComponent() returns null.

We created a test project to replicate this bug in order to easily share it with you.

The project consists of two scenes:

  • GarbageTestCaseScene - it has a GameObject with a component/behaviour calling GetComponent() multiple times per update while the asked component is missing.

  • NoGarbageTestCaseScene - it has the same component/behaviour but it asks for a component that exists on that GameObject.

To replicate the bug run the first scene and start profiling and you will see that a lot of garbage per frame is generated while on the other scene this doesn't happen.

This is the code of our behaviour calling GetComponent():

 public class GetComponentBehaviour : MonoBehaviour
 {
     void Update()
     {
         for (int i = 0; i < 5000; i++) {
             AnotherBehaviour component = this.GetComponent<AnotherBehaviour>();
         }
     }
 }
 

The question is, should this be happening? Why? We know it is not recommended to call GetComponent multiple times per update but we need it in some cases and we are willing to pay the CPU cost but not the garbage generation one.

The test cases were tested using Unity 4.3.4f1 pro and 4.3.2f1 free.

UPDATE: in case anyone wants to know, the issue case is 590565.

Comment
Add comment · Show 7
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 robertbu · Feb 10, 2014 at 05:13 PM 0
Share

Only a couple of people on this list work for Unity. You should report this as a bug:

http://unity3d.com/BugReportingFAQ

avatar image DaveA · Feb 10, 2014 at 05:18 PM 0
Share

Yes please report this bug using the Help menu in Unity

avatar image rubeng · Feb 10, 2014 at 06:08 PM 0
Share

In the how to post a bug question on this site (http://answers.unity3d.com/questions/9292/how-do-i-report-a-bug-in-unity.html) the user Nicolaj Schweitz from Unity, mentions that posting also here to see if the community can reproduce it is a good idea.

avatar image arielsan · Feb 10, 2014 at 06:11 PM 1
Share

Yeah, we already reported this bug but we are going to keep this question alive to update it if we receive any answer from Unity $$anonymous$$m and/or other people wants to share possible workarounds, etc.

avatar image arielsan · Feb 10, 2014 at 07:03 PM 1
Share

We already tested using the GetComponent(System.Type) and avoiding the cast and it still generates garbage, so we concluded that the problem is on the native side. The weird thing is, it generates garbage when it doesn't find the component and no garbage at all when the component exists. Also the garbage generated on the first scene during the profiling is so noticeable that it is affecting the process time compared to the second scene.

Show more comments

1 Reply

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

Answer by maxxa05 · Sep 11, 2014 at 10:33 PM

Oh, just read this post, it says:

This is why when you call GetComponent() to query for a component that doesn’t exist, that you see a C# memory allocation happening, because we are generating this custom warning string inside the newly allocated fake null object. This memory allocation does not happen in built games. This is a very good example why if you are profiling your game, you should always profile the actual standalone player or mobile player, and not profile the editor, since we do a lot of extra security / safety / usage checks in the editor to make your life easier, at the expense of some performance. When profiling for performance and memory allocations, never profile the editor, always profile the built game.

Seems like the null we see isn't really null, and it isn't generating garbage in built games. I guess I don't have to worry anymore then.

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Reasonable heap alloc. per second and total ? 0 Answers

Optimize (Garbage Collection, Memory usage) 1 Answer

ManagedHeap.ReservedUnusedSize 0 Answers

Efficient access to Mesh arrays 1 Answer

RenderTexture causes memory problems 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