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 /
  • Help Room /
avatar image
1
Question by TheSorm · Jul 14, 2016 at 02:32 PM · variablesnewgcgc.collect

gc allocated/ gc collect

Hey there, i have some issuce with my garbage collection, so i have some questions, 1: is it better to do this:

     for (int x = 0; x < List1.GetLength(0); x++) {
         for (int y = 0; y < List1.GetLength(1); y++) {
             List1[x, y] = null; // or new MyType()
         }
     }

than that:

         List1= new MyType[50, 50];

2: is it better to do this:

 Vector2 vec = new Vector2();
 
 void Update () {
          vec.x = 3;
          vec.y = 5;
          Funktion(vec);
 }

Than that:

     void Update () {
              Funktion(new Vector2(3,5));
     }

And how to solve this in a better way:

 public IEnumerator SpawnEnemys(int count, GameObject enemy) {
     int counter = 0;
     while (counter < count) {
         yield return new WaitForSeconds(Random.Range(5f, 10f));
         Instantiate(enemy, transform.position, Quaternion.identity);
         counter++;
     }
 }






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

Answer by Dave-Carlile · Jul 14, 2016 at 04:07 PM

  1. The first way you're only going to have garbage for the MyClass instances (assuming you're removing the only reference to them). The second way you'll have the garbage for MyClass as well as the list. So the first way is less garbage. But look up object pooling and pool your MyClass objects so you can re-use references to them rather than creating new ones. Of course, if you're only allocating them at transition points - e.g. between levels or something - then it's probably not much of an issue either way.

  2. Vectors are structs, and structs are (generally) allocated on the stack and aren't subject to garbage collection. So the second way is fine and more readable in many cases.

  3. WaitForSeconds is just a normal class, so you can create an instance of it once and reuse it. However if you want a random wait amount you would need to pre-create a bunch of them with various wait times and select one at random. See object pooling again.

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 TheSorm · Jul 14, 2016 at 04:32 PM 0
Share

First thanks for that nice answer :) 1: thanks i will look at object pooling :) 3: i dont mean the wait for seconds i mean the Instantiate, it created a lot of gcallocated..., but i think i can do this with object pooling too ^^

avatar image
-1

Answer by The-Masked-Fox · Jul 14, 2016 at 04:34 PM

No, and no. However, nothing you've posted here would have any impact on garbage collection. You should use the Profiler in Unity's editor to figure out which methods are creating garbage. There are several guides online for how to do that. Search for "Unity profiler tutorial" in Google.

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 TheSorm · Jul 14, 2016 at 10:12 PM 0
Share

If you would be better informed you would know that some of this things in Update Loops causes much data at garbege colllection... read Dave-Carlile post thats a good answer

avatar image Dave-Carlile · Jul 15, 2016 at 11:54 AM 1
Share

Good advice on using the Profiler to identify memory allocations.

avatar image TheSorm Dave-Carlile · Jul 15, 2016 at 04:23 PM 0
Share

haha yes oc :D

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

48 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 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

Simple Code Not Woking 1 Answer

Why too much GC after loading scenes for multi-times? 0 Answers

What is Image.Start() ?? and How can i reduce this function's GC.Collect 0 Answers

can someone give me a game to program 2 Answers

how do i change the direction of my player using the camera's Y rotation 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