• 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
7
Question by CHPedersen · Aug 05, 2011 at 08:20 AM · memoryprofiler

Memory Management and List

Hey all,

Inspired by this question on textures and their memory usage, I decided to spend a little time playing around with Unity and its memory usage, and I found several things that go against my understanding of how this is supposed to work, and frankly, I'm a little disturbed, and hoping someone can help me shed some light on what's going on. O_O

First off, the project is an empty testscene. The only GameObject present is a camera with a script attached. The script looks like this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     List<int> test = new List<int>();
 
     void OnGUI()
     {
         if (GUILayout.Button("Add items"))
         {
             for (int i = 0; i < 100000; i++)
             {
                 test.Add(i);
             }
             Debug.Log(test.Capacity);
         }
         if (GUILayout.Button("Delete items"))
         {
             //test = new List<int>();
             test.Clear();
             test.TrimExcess();
             System.GC.Collect();
             System.GC.WaitForPendingFinalizers();
             Debug.Log(test.Capacity);
         }
     }
 }

As you can see, the only thing the script does declare an empty List, then paint two buttons, one that lets me add 100.000 items and another that deletes and trims the list. I have two questions, both of which are memory related:

  1. When I run this scene in a freshly opened editor (Unity Pro 3.4), the total memory usage reported by the profiler stabilizes at 196.5MB (or so). When I stop, and then immediately click Play again, the reported total memory is 197.6. When I stop, then hit play again, it's 199.8. Then 201.2. Then 202.5. I can keep doing this as many times as I want. Why does the editor leak ~1MB memory every time I run a basic program in it? Is this because some left-over data from previous executions in the editor doesn't get garbage collected till much later, or something?

  2. About the list that gets manipulated by the buttons... I can cause the expected jumps in memory usage every time the runtime system doubles the List's capacity to make room for more items. That matches my expectations just fine. But no matter what I do, I can't seem to cause the memory to be freed again. According to MSDN, calling Clear and TrimExcess is supposed to first wipe the list, then set the capacity to the number of items (0, after clear), which reallocates the memory. I.e. if the capacity gets set to 0, it should free the memory previously allocated to List items, right? But it doesn't. I'm not seeing a drop in total memory in the profiler, and I'm not seeing a drop in Windows' Task Manager if I run a build outside the editor, either. Why doesn't TrimExcess free my memory? It makes no difference if I re-instantiate the entire list with the new-keyword. I'm still not getting my memory back. This doesn't make sense to me. :-/

UPDATE: I just tested this in a standard WinForm app with the same two buttons, using Microsoft's compiler and .Net runtime (MS Visual C#). The memory management is correct there. Clear+TrimExcess definitely wipes the list and frees the memory there. This behavior is something Unity/Mono.Net related.

Comment
Add comment · Show 10
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 Statement · Aug 05, 2011 at 09:38 AM 1
Share

Did you build a release standalone game?

avatar image Statement · Aug 05, 2011 at 09:39 AM 0
Share

Could also be something to do with log messages etc, I wouldn't rely on editor performance data.

avatar image CHPedersen · Aug 05, 2011 at 09:41 AM 0
Share

Yes. That's what I meant by "not seeing a drop in Windows' Task $$anonymous$$anager if I run a build outside the editor". ;)

avatar image Julien-Lynge · Nov 09, 2011 at 07:46 PM 0
Share

Christian, I'm glad someone else has seen this too, because I thought I was going crazy at first. I had the same issue with large lists of floats and Vector2s. I had a large list that I would populate until I had enough information to create a 65536 vert mesh, then I would instantiate it and move on to the next mesh. After every loop I would reassign to the same list, and I would very quickly get a giant memory leak. I tried clearing and setting null, but nothing helped. Eventually I changed the code to use float[] and Vector2[] ins$$anonymous$$d (there were reasons I didn't initially do this, but I was able to get around them). As soon as I did that, the memory leak disappeared.

BTW: this wasn't just an artifact of the editor or profiler. This would occur even in compiled exe versions, would appear in Windows Task $$anonymous$$anager, and would cause heap errors after a while of use.

avatar image J3-Gaming · Nov 09, 2011 at 08:16 PM 0
Share

Has anyone else noticed that the default namespace for List is Boo.Lang.List?

Is that implemented by $$anonymous$$ono or is this unrelated? The namespace for a List in .NET is System.Collections.Generic.List

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Show more comments

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by peter8989 · Aug 17, 2011 at 06:11 AM

i'm having the same problem, but its too terrible in my case. The profiler memory & the task manager memory both increases and the funny thing is i dont have anything in the scene, except the camera which has no script or anything. I deleted all the assets and cleaned my project explorer window to make sure my game has nothing but the camera. Still, 1MB everytime I run it. So, weird!!!!! Anyone has a solution?

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
  • ‹
  • 1
  • 2

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

iOS: Compressed audio not affecting memory? 2 Answers

iOS Vs New Memory Profiler 0 Answers

Setting Active Profiler to WindowsPlayer does nothing 0 Answers

When a animation is loaded to memory? 0 Answers

Instantiate fails on iPad. How can I debug memory allocation errors? 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