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
0
Question by Mrb83 · Jul 29, 2018 at 11:23 AM · memorygarbage-collectionheap

Is there a way to accurately measure heap allocations in unit tests?

Allocating temporary objects on the heap every frame in Unity is costly, and we all do our best to avoid this by caching heap objects and avoiding garbage generating functions. It's not always obvious when something will generate garbage though. For example:

 enum MyEnum {
   Zero,
   One,
   Two
 }
 
 List MyList = new List();
 MyList.Contains(MyEnum.Zero); // Generates garbage


MyList.Contains() generates garbage because the default equality comparer for List will use objects which causes boxing of the enum value types.

In order to prevent inadvertent heap allocations like these, I would like to be able to detect them in my unit tests.

I think there are 2 requirements for this:

  1. A function to return the amount of heap allocated memory

  2. A way to prevent garbage collection occurring during the test

    I haven't found a clean way to ensure #2. The closest thing I've found for #1 is GC.GetTotalMemory()

          [UnityTest]
             IEnumerator MyTest()
             {
               long before = GC.GetTotalMemory(false);
               const int numObjects = 1;
               for (int i = 0 ; i < numObjects; ++i)
               {
                 System.Version v = new System.Version();
               }
               long after = GC.GetTotalMemory(false);
               Assert.That(before == after); 
             }
     
    
    


The problem is that GC.GetTotalMemory() returns the same value before and after in this test. I suspect that Unity/Mono only allocates memory from the system heap in chunks, say 4kb, so you need to allocate <= 4kb before Unity/Mono will actually request more memory from the system heap, at which point GC.GetTotalMemory() will return a different value. I confirmed that if I change numObjects to 1000, GC.GetTotalMemory() returns different values for before and after.


So in summary, 1. how can i accurately get amount of heap allocated memory, accurate to the byte and 2. can the garbage collector run during the body of my test, and if so, is there any non-hacky way of disabling GC for the duration of my test?

TL;DR
Thanks for your help!

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

Answer by Bunny83 · Jul 29, 2018 at 06:27 PM

No, there is no reliable method inside the runtime. GetTotalMemoty returns:

A number that is the best available approximation of the number of bytes currently allocated in managed memory.

The actual numbers are only available to the runtime itself. However when you use the Unity profiler to figure out what is going on regarding memory allocations. The profiler hooks into most methods. Keep in mind that the profiler has a "deep mode" which shows almost a line by line report for each method called (actually more a method by method report). Note that the managed memory allocated by a method is summarized into one "GC.Alloc". If you want to further narrow down the memory of a particular section you can use:

 UnityEngine.Profiling.Profiler.BeginSample("SomeUniqueName");
 
 // The code you want to profile
 
 UnityEngine.Profiling.Profiler.EndSample();

To get a seperate sample entry in the profiler. You can easily search for that name in the profiler so you don't need to find the frame manually and don't need to trace the callstack manually. Once you have selected your sample you can remove the search string and your sample should be selected in the normal view.


Preventing garbage collection was introduced in NET 4.6 so depending on your Unity version, script backend and .NET version selection it may not be available to you.


So in short: No, there is no way to manually determine how much memory was allocated within a certain code section through code. And no you can't really suppress garbage collection apart from the fact that it wouldn't help. So you can't incoperate memory allocation into unit tests.


Just as a side note: The System.Version class contains just 4 int values. This is 16 bytes in total. However each class instance has some additional overhead like the type pointer, vtable and maybe some memory alignment. At least in my tests one Version instance requires 32bytes of memory.


About your first example: Yes, Mono has some bad habits / implementations here. In normal .NET the default comparer actually creates a proper generic comparer (based on IEquatable<T>) to get a garbage free comparison. Mono has several of these poor implementations (same goes for foreach and IDisposable for struct enumerators). Though this is implementation dependent behaviour that you can't catch with unit tests in the editor properly.

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 Mrb83 · Aug 12, 2018 at 06:57 PM 0
Share

Thanks for taking the time to answer.

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

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

Unity iOS controlling heap size growth 1 Answer

Memory reserved size really large 0 Answers

How to see mono heap or garbage collected objects? 0 Answers

How important is it to wrap temp variables/objects in a "using" block? 4 Answers

calling www to get audio.clip multiple times causes memory problems 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