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
2
Question by kromenak · Feb 15, 2019 at 07:58 PM · memoryprofilingbinarywriter

Why does BinaryWriter allocate memory when writing floats?

I'm writing a quick save/load system, so you can basically tap "F5" and we use a MemoryStream/BinaryWriter combo to write out the game's state. I'm reusing a single byte[] array as a buffer, in an attempt to not do a bunch of allocation every time a save occurs.


This all works great, but I'm seeing a lot of "GC.Alloc" rows appearing in the profiler. Upon further inspection, I found that there is one "GC.Alloc" call per call to BinaryWriter.Write(float), and each of these calls allocates 36B of memory. Floats are fairly common, so this ends up adding up quite a bit, to ultimately 152KB of memory allocation!


Upon further inspection, it's possible for me to pack the float value into a "uint" (since they are both 4 bytes), and I no longer get that 36B allocation.

 writer.Write(myFloat); // allocates 36B
 writer.Write(ByteUtil.FloatToUInt(myFloat)); // allocates 0B


I guess ultimately my question is this: is it expected that BinaryWriter.Write(float) would allocate memory like this? Is it expected behavior, or is this a bug? Is there any way to know whether this is something that should be reported as a bug to Unity?

(Note: sorry for the horizontal rules, but this site is not properly formatting my paragraphs for some reason...)

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
4
Best Answer

Answer by Bunny83 · Feb 15, 2019 at 09:07 PM

That's an interesting and rather unexpected observation. However it seems that Mono simply implemented the Write method this way for floats. Note that the mono version uses the "BitConverterLE.GetBytes" method which returns a new 4 byte array. The code in the "else" section is the code used by the .NET framework.


I don't know that ByteUtil class you're using. However one reason why they might use the BitConverterLE is to ensure little endian byte order. The BinaryWriter always uses little endian byte order to ensure compatibility with other systems. Otherwise when you store some value on a little endian system and read it on a big endian system, the values would come out the wrong way. So you have to ensure you use the same format everytime.


Just to be clear: This is not a bug as nowhere is it mentioned that the Write method doesn't generate garbage. It's just an unfortunate implementation You can roll your own extension methods for the BinaryWriter / Reader, just make sure the endianness is taken care of.

Comment
Add comment · Show 1 · 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 kromenak · Feb 17, 2019 at 12:03 AM 0
Share

Oh interesting - I didn't realize the source for these functions were available, I'll have to bookmark those links. VS assembly browser just showed empty functions.

When you compile and run Unity code with IL2CPP, is it always using the "#if $$anonymous$$ONO" versions of functions, as far as you're aware? Unity has several player options, and I'm currently using .NET 4.x Equivalent, IL2CPP, .NET Standard 2.0.

The source for the ByteUtil is doing basically what the .NET source is doing. I'll keep an eye out for any endian issues that might arise! Thanks for the info.

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

103 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Can I get the "Total Allocated" memory from script? 1 Answer

Help Regarding C# Objects Memory Profiling 0 Answers

Profiling static memory usage 0 Answers

Memory profiler freezes editor/crashes build on device 0 Answers

Setting Active Profiler to WindowsPlayer does nothing 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