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
3
Question by JeffBert · Sep 30, 2015 at 09:05 PM · c#eventsactiongarbage-collection

Does passing an Action as a parameter create garbage?

I'm using a "timer" script that takes an action as a parameter. The script calls the Action when the timer is complete. Does passing an action in a parameter create garbage? I use this a lot and im getting a bunch of GC.Collect() calls in the profiler and it's coming from my timer classes.

Here's what it looks like:

 public class Timer : MonoBehaviour
 {
     private float _timer;
     private float _maxTime;
     public void StartTimer(float time, Action callback) 
     { 
          _timer = 0;
         _maxTime = time;
         TimerCompleteCallback = callback;  
     }
     ////////// Some update code //////////////////////
     void TimerEnd() 
     {  
         this.enabled = false;
         TimerCompleteCallback();
     }
 }
 
 public class OtherClass : MonoBehaviour
 {
      private Timer _timer;
      void Start()
      {
          _timer = gameObject.AddComponent<Timer>();
          _timer.StartTimer(5, OnTimerComplete);
      }
                 
     void OnTimerComplete()
     {
         // Will this timer loop end up creating garbage?
         _timer.StartTimer(5, OnTimerComplete);
     }
 }

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
6
Best Answer

Answer by landon912 · Sep 30, 2015 at 11:40 PM

Fun! Here is your issue:

 _timer.StartTimer(5, OnTimerComplete);

This seems to be inlined to be:

 _timer.StartTimer(5, () => OnTimerComplete);

Therefore this is creating a new delegate each time you start the timer. If you cache the delegate this is solved:

 public class OtherClass : MonoBehaviour
 {
       private Timer _timer;
       private Action _action;
 
       void Start()
       {
           _timer = gameObject.AddComponent<Timer>();
          _action = OnTimerComplete;
           _timer.StartTimer(5, _action);
       }
                  
      void OnTimerComplete()
      {
          // Will this timer loop end up creating garbage?
          _timer.StartTimer(5, _action);
      }
  }
 

Hope this helps!

For reference, here is my test:

 using System;
 using UnityEngine;
 
 public class ActionDelegateGarbageTest : MonoBehaviour
 {
     private Action mAction;
 
     public void Start ()
     {
         mAction = () => EventReciever ();
     }
 
     public void Update ()
     {
         //doesn't create garbage
         //PassTest (mAction);
 
         //creates garbage
         //PassTest (EventReciever);
     }
 
     private void PassTest (Action actionToComplete)
     {
         actionToComplete();
     }
 
     private void EventReciever()
     {
         
     }
 }

Comment
Add comment · Show 1 · 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 laessnb · Mar 12, 2021 at 04:22 PM 1
Share

Thank you from 2021. I was racking my brain trying to figure out the same issue!

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

30 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Add an iteration of OnClick methods 1 Answer

When to use 'delegate', 'event' or 'Action' ? 1 Answer

Adding a function to a UI 4.6 Button called on another game object in c# 0 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