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
1
Question by TowerOfBricks · Jan 23, 2011 at 10:08 AM · arrayoptimizationclassmemory

Array Allocations, references or whole objects

Hi

Let's say I have an array int[100], this would have to allocate
4 (4 bytes in an int) * 100 (length) = 400 bytes (plus a few bytes for type and length info).
Am I right?

However if I declare an array like: Vector3[100], this would allocate:
4*3 (4 bytes in a float, 3 floats in a Vector3) * 100 = 1200 bytes
Correct?

But now to the real question, if I create an array of class objects. I have my custom class

public class MyClass {
   int x = 123;
   int y = 456;
}

And if I create an array with one 100 of those class objects, MyClass[100], would this allocate:
4*2 (two ints) * 100 = 800 bytes (plus a few bytes for type and length info)
or would it only allocate:
4 * 100 = 400 bytes
For the references to the MyClass objects, and the allocation for the MyClass objects would actually be done when calling new MyClass (...);

Thanks in advance, Aron

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 johan-skold · Jan 23, 2011 at 10:22 AM

Classes are reference objects in C#, so it would only allocate the 400 bytes required to hold references. If your object had been a struct, it would have been a value type and would have allocated the full 800 bytes.

Comment
Add comment · Show 7 · 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 Statement · Jan 23, 2011 at 12:01 PM 0
Share

Also remember that the instances of $$anonymous$$yClass can become scattered across the heap resulting in cache misses iterating through the lot. This is because there is no guarantee your instances line up perfectly. Using a struct would, however. For this reason, many lightweight types are structs.

avatar image TowerOfBricks · Jan 23, 2011 at 02:10 PM 0
Share

Ok, so the follow-up question, can I allocate classes so they don't get scattered around? I can't use structs since I need to use references within the object.

avatar image TowerOfBricks · Jan 23, 2011 at 02:12 PM 0
Share

And also, "lightweight", how many bytes is that? I have a class using 30 bytes or so, but I have something like 10000+ of them in one array.

avatar image johan-skold · Jan 24, 2011 at 03:47 PM 0
Share

If you iterate over them regularly, a struct is surely the way to go. Note that you can pass a struct by reference to methods even if it's a value type. I am unsure if this causes boxing internally but I doubt it. To pass a struct by reference, you specify the parameter with the ref keyword in both the declaration and invocation. Such as "void $$anonymous$$y$$anonymous$$ethod(ref $$anonymous$$yValueType object)" and "this.$$anonymous$$y$$anonymous$$ethod(ref myObject);"

avatar image TowerOfBricks · Jan 24, 2011 at 05:20 PM 0
Share

Is there a way to store a reference to a struct, they are usually passed by value, but that wouldn't work in all cases for me. C++ got pointers, but I haven't found something like that in C#.

Show more comments

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

No one has followed this question yet.

Related Questions

Need help setting an array of my class 0 Answers

Reasonable heap alloc. per second and total ? 0 Answers

an array of a class, in another array of a class 2 Answers

Creating a dynamic array of objects of custom class 1 Answer

Making a GetComponent array 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