UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

optimization

Double Array Memory Discrepancy

Under Audit

memory allocationdata structures

Unity 2021.3.x - Unity 6.1.x

Published 8 days ago

A double array's reported byte size can be unexpectedly larger than its calculated value. This often stems from memory overheads in the runtime environment.

Issue

 The reported byte size of a **double**[65536] array, when observed through tools like FileInfo, appears significantly larger than its theoretically calculated size. A **double** array of this specific dimension is expected to occupy approximately 0.5 MB (65536 elements multiplied by 8 bytes per double element). However, measurements indicate a size closer to 1.5 MB, implying an additional overhead of 16 bytes per element. This considerable discrepancy in the actual versus expected byte size creates confusion regarding the memory footprint of **double** arrays.

Experimental Fixes
  • Profile memory using the Memory Profiler package to identify specific overheads.
  • Ensure the data is stored as a raw double[] and not implicitly converted or boxed into other object types, which can incur additional object header overhead.
  • Consider using a NativeArray<double> when working with the Job System or burst-compiled code, as these often have more predictable and tightly packed memory layouts.

Editor's Note:

The above fixes have not been verified by our audit team yet. They are provided exclusively for your own technical research. We recommend creating a backup of your project before proceeding with any attempts. Utilize at your own discretion!

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.