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 TheSmokingGnu · Feb 18, 2018 at 04:21 PM · sizememorymonobehaviourmemory usage

Whay is the size of MonoBehaviour instance?

I can't find a way to access the size(in bytes) of the managed C# objects.

What i would like to know specifically, is how much an instance of monobehaviour takes, and how much of that memory is added when you create your own MonoBehaviour-subclass instance?

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
3

Answer by Bunny83 · Feb 20, 2018 at 02:31 PM

Well generally the size of a managed class is just the sum of it's fields + "some overhead". While the sum of the fields could be determined by enumerating all fields of a type the "overhead" can't be specified exactly as it's not necessarily constant. First of all you have to define what you consider part of the object. For example if an object has an array, that array is a seperate object. A string value is also a seperate object. So the bare class itself is rather small.


Each managed class instance has some generic overhead which however depends on the systems architecture (32 / 64 bit) and the actual implementation of the CLR for the target system. Running the same code and using the same classes on different systems can result in different sizes. Memory address alignment can also play a role. Generally on 32 bit systems the fix overhead for a class instance is 8 bytes and on 64 bit 16. However this is just the overhead. The minimum size is actually 12 bytes on 32 bits and 24 bytes on 64 bit (even an empty class).


The MonoBehaviour class itself as well as Behaviour and Component do not have any fields on their own. However the UnityEngine.Object base class does have 3 fields. One IntPtr, one int and one string variable. IntPtr and string require just the defaut reference / pointer size (so 4 bytes on 32 bit and 8 bytes on 64bit system). An "int" which is just an alias for System.Int32 is always 32 bits (4 bytes). So just looking at the size of the plain managed object instance it's at least 8 + 4 + 4 +4 == 20 bytes on 32 bit systems and 16 + 4 + 8 + 8 == 36 bytes on 64 bit systems.

So we talk about a class like this:

 public class MyMonoBehaviour : MonoBehaviour
 {
 }

No extra fields or anything. Though that's not the end of the story. Those 20 / 36 bytes is just the plain managed object. However every component has a native counter part which is basically referenced by the internal IntPtr field in UnityEngine.Object. We don't know what exactly will be stored in the native part as it's closed source and part of the engines core. Though every UnityEngine.Object has a name. It doesn't need to be set to anything but you would require at least one pointer. Each instance has a hideflags int field. The Behaviour class implements an enabled field on the native side. The Component native part has at least a pointer to the owner (gameobject). Also it seems UnityEngine.Object has two additional pointers to manage prefab references. However it's possible that those only exists in the editor.


Note that we only considered direct fields of the managed class itsef or it's native counter part. We haven't included any child objects which you may want to consider to be part of the object (composition). This would include string data for the name and maybe some other internal things. The native part of a MonoBehaviour also has a reference to the script asset that defines the managed class.


All in all you have to calculate with an actual memory (native + managed) of something between 70 and 200 bytes. Though memory alignment and memory allocation by the managed memory manager would make it difficult to give exact numbers.


I also don't fully understand this part:

[ ... ]and how much of that memory is added [ ... ]

When an object instance is created all required memory is allocated. But again, only the direct memory usage of the class itself. For example consider this class

 public class MyMonoBehaviour : MonoBehaviour
 {
     private int[] someValues = new int[100];
     private string test = "Hello World";
 }

This class now has an array and a string field. An array as well as a string is a seperate object and does not "belong" to the MyMonoBehaviour class. By adding those fields the size of the class itself will grow by 8 bytes on 32 bit and by 16 bytes on 64 bit systems. However the actual array instance also requires memory. Specifically for arrays of value types it'S the usual 8 byte overhead + 4 bytes length + the size of an element multiplied by the length of the array. So about 12+100*4==412 bytes. Strings work similar but as far as i remember they have some weird 4 byte alignment going on. A string is an array of chars. A char is 16 bit (2 bytes)


So in the example above if we attach this script to a gameobject we have the size of the class itself (which as i said we can't really determine exactly) + the array instance + the string instance.


The main question is: Why is it that important? What do you want to do?

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
avatar image
0

Answer by Kishotta · Feb 18, 2018 at 11:08 PM

Are you looking for the sizeof keyword?

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 TheSmokingGnu · Feb 19, 2018 at 05:31 PM 0
Share

No, i am not: the sizeof keyword works only with value-type data, while classes are reference-types.

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

78 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

Related Questions

Is this memory usage too much for a mobile game? 1 Answer

What does Profiler.GetReservedMemory() return? How is it determined? 0 Answers

Apk Setup File Size 4 Answers

Texture Doubled in size in RAM Profiler 3 Answers

Does anyone know why Unity is using 7GB of memory? 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