Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AndyMartin458 · Mar 25, 2013 at 12:41 AM · c#memoryclassesbyte

Does variable order affect class size due to byte alignment in C#/Unity

I've studied recently that in C++ the order of your variables can ultimately affect the memory of your class due to byte alignment. Is the same true for our classes in C#?

 class memInefficient{
     int i1;  //It's safer to put the 2 bool vars at the end?
     bool b1;
     bool b2;
     int i2;
 }
Comment
Add comment · Show 7
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 Unitraxx · Mar 25, 2013 at 01:38 AM 0
Share

Although it probably happens in C# too, as with C++ it's not at all a problem. If you really care about the small portion of memory that gets "wasted" just try to group all the variables of the same type together, $$anonymous$$imilazing those gaps.

avatar image Tarlius · Mar 25, 2013 at 03:39 AM 0
Share

Where did you read this? I've never heard of anything of the sort :s

As far as I'm aware, bools are just glorified ints.

Either way, if this was this case, I would be absolutely shocked if the compiler didn't do this for you.

avatar image AndyMartin458 · Mar 25, 2013 at 04:07 AM 0
Share

@Tarlius It's definitely true in C++ in Visual Studio 2010. You can try by making a class and then checking it's size with sizeof(myClass).

avatar image AndyMartin458 · Mar 25, 2013 at 04:09 AM 0
Share

It was more of a question of "does it happen" as opposed to "does it matter." I suppose I'll just have to check and see if it is affected by the alignment. I suppose my question was more of "Does Unity have any suggestions for variable ordering?"

avatar image Tarlius · Mar 25, 2013 at 04:42 AM 1
Share

Googled it and found some supporting evidence. Pretty shocked, never heard of this before! I guess it makes sense in c++ though since you can do weird things like stepping over classes with offsets from the pointer to the class if I recall correctly. For that to work the compiler would have to be outputting "defined" class structures. Optimising them would break any code doing that :/

As for how this is in unity, if anyone checks it out I'd be interested in results, although I think for most cases it'd be better focusing on readability. Genuinely curious whether the compiler would do this for you though. I believe that one of the supposed benefits of JIT code is that it can be optimised for the platform its running on at run time, and alignment looks like it would be viable candidate. Since you can't know the size of the primitives until runtime, and c# doesn't let you use "real" pointers (although theres an unsafe mode I've no experience with) its possible c# could do this behind the scenes...

Although in the inspector all public/serialised variables are displayed in the same order as the code, so maybe not... :o

Show more comments

1 Reply

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

Answer by AndyMartin458 · Nov 13, 2013 at 09:07 PM

I created a few structs with different organization of the variables. It appears to me that the arrangement does not affect the struct when I used Marshal.SizeOf(). It really is a letdown that I can't use sizeof on a class. It is nearly certain that C# takes care of the variable memory alignment for you.

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

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Variables inside class not showing up in Inspector C#. 2 Answers

How to refer to object based on value of a variable 1 Answer

How do you profile memory usage during compilation? 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