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
1
Question by Julber · Nov 15, 2016 at 03:16 PM · listgc

Why is GC collecting list with capacity?

Hi. I have a scene with one object that uses one script called "Test". Problem is that the following code creates 488,4 KB of garbage for the GC if I construct the list with capacity. Yet, I need to set capacity somewhere to avoid resizing of the list if I add stuff to it. I've tried to find reasons for GC alloc like by making a bool member and setting it values of the list (like in for loop, member = v[i]) in Test.Update() to give a reason for v to exist and not be deallocated. Deep profiling shows that GC collection happens in Start() already, also without the for loop.

Here's the code:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class Test : MonoBehaviour {
     List<bool> v;
     
     void Start () {
         v = new List<bool>(500000);
 
         for (int i = 0; i < v.Capacity; i++)
         {
             v.Add(false);
         }
     }
     
     void Update () {
 
     }
 }


In my game project I want to keep a list to store data but fill it later after I know what the data is. So the list is empty at first and I'm initializing (and giving capacity) for members in Awake().

So, why does list "v" cause GC alloc and how do I avoid that but still could set capacity?

Thanks!

Comment
Add comment · Show 3
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 TreyH · Nov 15, 2016 at 04:27 PM 0
Share

According to the compiler, that is a private list that is assigned once and never used. Why should it keep it around?

avatar image Julber · Nov 15, 2016 at 05:56 PM 0
Share

@TreyH Ok. But why does compiler think the list is not used if I use it in Update()? I made a for loop, printed out values of list like v[i] and also tried reassigning a new value to every element in list because I suspected referencing issue too. Setting it to public didn't work either but I haven't tried to access it from other script yet. If it makes difference.

Btw. If I print v.Capacity in Update(), output is "500000" even though profiler shows GC alloc as 488,4$$anonymous$$B in Test.Start()

avatar image Bunny83 Julber · Nov 17, 2016 at 11:57 PM 0
Share

btw:

500 $$anonymous$$B == 488.28125 $$anonymous$$iB

So it actually allocated a bit more than the 500000 required bytes. A List has some additional data ("Count" as well as a managed reference to the actual array. Also that array itself also has the internal "Length" stored in memory).

1 Reply

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

Answer by Bunny83 · Nov 15, 2016 at 06:09 PM

I think you're confusing allocation with collection of garbage. As seen from the GC every bit of memory is potential garbage. Whenever you allocate memory the GC will keep track of everything. Of course when you execute this line:

 v = new List<bool>(500000);

You will allocate about 500k of memory. However that memory is not collected as it's still in use. When garbage memory is collected it's always done from the GC-thread and never from the main thread.

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 Julber · Nov 15, 2016 at 07:12 PM 0
Share

@Bunny83 Oh no...lol I'm pretty embarassed right now... So "GC alloc" doesn't mean the amount of collected memory but actually allocated heap memory.

I'm pretty sure someone's been wondering if my question was just written by "a troll" but I've spent like 2 days with this... =)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Find GameObjects with a certain Script [Solved] 1 Answer

print the whole list to screen 1 Answer

Find the differences between two lists 1 Answer

Show and Edit Attributes of my list of structs in EditorWindow. 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