Unity Memory Management

5 min. read

Unity Memory Management

Calculations
File Size
[(Height in pixels) x (length in pixels) x (bit depth)] / 8 / 1024 = image size in kilobytes

Multiply the number of horizontal pixels by the number of vertical pixels to get the total number of pixels
Multiply the total number of pixels by the bit depth of the image (16 bit, 14 bit etc.) to get the total number of bits of data.
Dividing the total number of bits by 8 equals the file size in bytes.
Divide the number of bytes by 1024 to get the file size in kilobytes. Divide by 1024 again and get the file size in megabytes.
2048 x 2048 = 4,194,304
4,194,304 pixels X 16 bit = 67,108,864 ÷ 8bits = 8,388,608 Bytes ÷1024 = 8,192 Kilobytes ÷ 1024 = 8 Megabytes

1,920 x 1536 = 2949120 (2.95 Megapixel Detector)

2,949,120 X 16 bit = 47185920 ÷ 8bits = 5,898,240 Bytes 5,760 Kilobytes ÷ 1024 = 5.625 Megabytes)

Of Pixels X Bit Depth ÷ 8 ÷ 1024 ÷ 1024 = File Size in Megabytes (MB)

RAM
width * height * bytes per pixel.

Debug.Log (“Total allocated memory : “ + System.GC.GetTotalMemory (0) + “ bytes”);

WIDTH x HEIGHT x (BITS PER PIXEL) = memory needed to load the full image

Image W x Image H x color depth = VAL_A
VAL_A / 8 = VAL_B
VAL_B / 1024 = Memory in kB

A square 4k image that’s 32-bit (8 bit per channel, RGBA) will use:
4096 * 4096 = 16,777,216 pixels
8 bits per channel * 4 channels = 32 bit = 4 bytes
4 * 16,777,216 = 67,108,864 bytes = 64 megabytes

S3TC (-> DXT) and PVR get their massive size reduction only due to their pot, because the compression compresses blocks of 2x2 pixels / 4x4 pixels etc.

if you aren’t pot, you can logically not compress such a texture at all.

DXT must be power of 4 side length, must not be square
PVR must be power of 2 side length, must be square

Power-of-Two Valid Sizes

Tools
https://assetstore.unity.com/packages/tools/utilities/resource-checker-3224

https://github.com/MarkUnity/AssetAuditor

References and Resources
https://blogs.unity3d.com/2018/06/27/new-best-practice-guide-memory-management-in-unity/

https://www.futurelearn.com/courses/representing-data-with-images-and-sound/0/steps/53142

https://www.scirra.com/manual/183/memory-usage

https://docs.unity3d.com/520/Documentation/Manual/class-TextureImporter.html

https://docs.unity3d.com/Manual/OptimizingGraphicsPerformance.html

https://gamedev.stackexchange.com/questions/7927/should-i-use-textures-not-sized-to-a-power
-of-2

https://www.opengl.org/wiki_132/index.php/NPOT_Texture

https://docs.unity3d.com/Manual/HOWTO-ArtAssetBestPracticeGuide.html

https://www.construct.net/en/blogs/construct-official-blog-1/remember-not-waste-memory-796

https://docs.unity3d.com/Manual/render-pipelines.html

https://learn.unity.com/tutorial/memory-management-in-unity

https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity2.html?_ga=2.8116539.887568085.1582834710-2104681041.1556939816

https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity.html?_ga=2.262517893.887568085.1582834710-2104681041.1556939816

https://learn.unity.com/tutorial/assets-resources-and-assetbundles

https://learn.unity.com/tutorial/optimizing-unity-ui

https://learn.unity.com/tutorials

https://docs.unity3d.com/Manual/BestPracticeMakingBelievableVisuals.html?_ga=2.262517893.887568085.1582834710-2104681041.1556939816

https://blogs.unity3d.com/2015/12/14/enterprise-support-solving-hard-problems/
https://answers.unity.com/questions/215761/does-unity-care-is-a-texture-is-a-power-of-2.html

https://docs.unity3d.com/Manual/class-TextureImporter.html?_ga=2.160481845.948286446.1582835216-1140446945.1557377407

https://answers.unity.com/questions/560187/texture-is-not-setup-with-power-of-2-how-to-do-so.html

https://answers.unity.com/questions/39497/non-power-of-2-textures-how-to-access-original-siz.html

https://forum.unity.com/threads/video-memory-allocation-issue.245146/

https://answers.unity.com/questions/1405613/unity-sprite-memory-usage-is-huge.html

https://assetstore.unity.com/packages/tools/utilities/resource-checker-3224

https://answers.unity.com/questions/998482/texture2d-get-file-size-btyes-runtime.html

https://answers.unity.com/questions/36059/is-there-a-way-to-know-the-system-memory-used-by-a.html

https://answers.unity.com/questions/324112/image-size-correlation-between-disk-and-memory.html

https://answers.unity.com/questions/1263894/image-size-increase-on-loading-in-memory-from-loca.html

https://forum.unity.com/threads/best-way-to-compress-non-power-of-2-image.396178/

https://forum.unity.com/threads/power-of-2-texture-sizes-best-practice.609538/