Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by code-blep · Jan 01, 2015 at 10:50 AM · dlllibrarygarbage collection

Creating Library - Worried about garbage collection

Hi and happy new year!,

I have decided to create my own go to library of common functions that I have written and would like to reuse in an easy to use package in my projects. Sudo code example:

 using UnityEngine;
 using System.Collections;
 
 public class mathSolutions : MonoBehaviour {
     
     public class mathSolutions : MonoBehaviour {
         
         public static Vector2 someVector2Function(Transform object1, Transform object2)
         {
             
             Vector3 variable1;
             Vector3 variable2;
             float variable3;
             
             // Other sudo code
             
             return new Vector2(result1, result2);
         }
         
         
         public static Vector3 someVector2Function(Transform object3, Transform object4)
         {
             
             Vector3 variable4;
             Vector3 variable5;
             float variable6;
             
             // Other sudo code
             
             return new Vector3(result1, result2, result3);
         }
     }
 }

So for example I could import the above script into Unity, and access the functions like this:

 mathSolutions.someVector2Function(player, target);

However, I am worried about garbage collection as I am declaring my variables in the functions. Am I right in thinking that this method will increase garbage collection? Any suggestions on best practice for this?

I have read that it might be best to store each of my own functions in their own script and compile them to a DLL, that way I can pre-declare the variables at the start of the script, and avoid having the library script getting massive over time. Would this be the better option?

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

1 Reply

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

Answer by Landern · Jan 01, 2015 at 11:11 AM

Generally speaking static method variables are on the stack per call. They are out of scope by the time you end or exit the method, when you pass reference types in, thats all they are, a pointer to something, if you instantiate a new object that goes out of scope, that will be placed in Gen0 for collection.

Read some about GC here

Comment
Add comment · Show 3 · 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 code-blep · Jan 01, 2015 at 12:06 PM 0
Share

Hi Landern,

That's a little bit over my head, and trying to understand the info in the link you provided, do I understand correctly that the example:

 return new Vector3(result1, result2, result3);

Will be the part that results in garbage collection?

avatar image Landern · Jan 01, 2015 at 12:14 PM 0
Share

Probably not, the stack frame should be reclaimed, you're returning a new Vector3 to most likely replace another Vector3 variable. You're worrying about things that you probably shouldn't.

avatar image code-blep · Jan 01, 2015 at 12:16 PM 1
Share

I was wondering if I was over thinking things ;) Still, the link you provided makes for interesting reading. Thanks for putting me straight Landern.

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

26 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

Related Questions

Can Unity track the changes I made to my DLL? 0 Answers

How to link to scripts in the Package Manager when building a dll. 0 Answers

The classes in the module cannot be loaded? 0 Answers

Is Unity not compatible with this library? 0 Answers

Obfuscating DLL classes from the users 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