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
3
Question by robinking · Jun 23, 2011 at 08:40 PM · memorystruct

How do I calculate how big my struct is?

I've read it's a good idea to keep one's structs within 16 bytes. I have this struct:

 public struct Astarnode {
     
     public byte x, y;
     public short g, h;
     public short f { get { return (short)(g+h); } }
     public bool walkable, open, closed, onPath;
     public byte2D parent;
     
     public Astarnode( byte a, byte b ) {
         x = a; y = b;
         g = 0; h = 0;
         walkable = true;
         open = false;
         closed = false;
         onPath = false;
         parent = new byte2D(a,b);
     }
     
     public void Clear() {
         g = 0;
         h = 0;
         open = false;
         closed = false;
         onPath = false;
         parent = new byte2D(x,y);
     }
     
     public bool HasParent() {
         return parent != new byte2D(x,y);
     }
     
 }

Am I right in thinking this struct is 12 bytes large, even if it has methods etc? (2 bytes, 2 shorts, 4 bools, 1 byte2D [which is another struct containing 2 bytes]) (and am I right in thinking Asarnode.f consumes no bytes as it is a property that returns the sum of two other fields?)

Comment
Add comment · Show 1
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 alexzzzz · Apr 14, 2012 at 02:56 PM 0
Share
 using System.Runtime.InteropServices;
 
 struct Foo
 {
     private int a;
     private byte b;
 }
 
 ...
 
 int size = $$anonymous$$arshal.SizeOf(typeof (Foo));

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Jun 24, 2011 at 12:07 AM

You can try using System.GC.GetTotalMemory(), allocate the struct, do System.GC.GetTotalMemory() again, and compare the difference.

Comment
Add comment · Show 4 · 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 robinking · Jun 25, 2011 at 05:55 PM 0
Share

I get 0 doing that?

avatar image Eric5h5 · Jun 25, 2011 at 06:25 PM 0
Share

@Robin $$anonymous$$ing: You might have to allocate a bunch. I just tried this:

 var amount = 1020;
 var mem = System.GC.GetTotal$$anonymous$$emory(false);
 var structs = new Quaternion[amount];
 Debug.Log ((System.GC.GetTotal$$anonymous$$emory(false) - mem)/amount);

That gives me 16, which I'd expect. Interestingly, if the amount is a little over 1020, I get 20 ins$$anonymous$$d, though it goes back to 16 if I use an amount of 2040. Anyway, an amount of 1020 seems accurate, since that also gives me 4 for float, 8 for Vector2, and 12 for Vector3.

Using Astarnode, I get 12. So yeah. :)

avatar image robinking · Jun 27, 2011 at 09:21 AM 0
Share

Thanks - I tried that and got the same! Interestingly, the "amount" seems to be different depending on how big no$$anonymous$$ally the struct is. When I added one more bool, it still reported 12, but some different amounts reported 13. If I have time to kill sometime, I might put together a chart of which "amount" figures report struct size accurately for which no$$anonymous$$al sizes of struct...

avatar image CHPedersen · Jun 27, 2011 at 10:39 AM 0
Share

Nice, I didn't know you could measure a program's memory impact with that GC method. I thought the only way was to use the profiler. +1 from me. :)

avatar image
-1

Answer by Marnix · Jun 23, 2011 at 08:44 PM

I don't know if you could really argue like this in Unity. People told me that Unity parses this to some native code, like C++ or objective C or whatever language is needed on the platform.

So I don't think you can calculate your bits and bytes just like that? Or if you can, why not try a simple `sizeof`?

Comment
Add comment · Show 3 · 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 Eric5h5 · Jun 23, 2011 at 09:21 PM 0
Share

C++ or Objective-C aren't native code, those are compiled languages. $$anonymous$$ono code, like .NET, is compiled to CIL (Common Intermediate Language), which is then JIT compiled into native code by the runtime, except on mobile platforms, where it's AOT compiled.

Anyway, sizeof is correct. You can sort of calculate bytes just like that (a byte is always a byte, a short is always two bytes, etc.), although there is some overhead in a struct like member field alignment.

avatar image robinking · Jun 23, 2011 at 11:38 PM 0
Share

I can't seem to get sizeof to work, as I need to compile in unsafe mode - how do I go about that?

avatar image Eric5h5 · Jun 24, 2011 at 12:07 AM 1
Share

@Robin $$anonymous$$ing: oh, right, I forgot that it only works with built-in 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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Optimization on list handling 2 Answers

Best way to calculate sum of custom object holding numbers and returning sum as that object instance 0 Answers

Why does OPENGL use 4 to 8 times the vram compared to DirectX 2 Answers

Random question about getcomponent 1 Answer

What happens when current scene is closing? 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