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
0
Question by Stone-Legion · Aug 14, 2014 at 09:20 PM · memorymemory managementmemory-leak

200MB file save causing unity memory to increase by over a gigabyte

Why is it that this line of code to save a 200MB sized object causes unity in memory to increase over 1GB? Before running this, unity memory usage sits at about 800MB while running my game within the editor. As soon as I serialize and save the game data, the memory usage skyrockets. I've verified there is literally only 200MB being saved to the file but when unity memory usage gets this high, it's prone to crash. I see no reason why memory usage would increase so much, the stream is disposed and closed. It is redundant for me to even use stream.dispose because close disposes it but just wanted to double check.

File Manager Class:

 public void WriteStream(string saveLocation, object o)
 {
     FileStream fs = new FileStream(saveLocation, FileMode.Create);
     BinaryFormatter formatter = new BinaryFormatter();
     formatter.Serialize(fs, o);
     fs.Close();
 }

From another class:

     FileManager fm = new FileManager();
     for(int i = 0; i < tileData.Length; i++)
     {
         string path = appDataFolder + @"\"+levelName+"layer"+i.ToString()+".dat";
         fm.WriteStream(path, tileData[i]);
     }

Serializable object that is being saved (tileData above):

 [Serializable]
 public class TileMapData : ISerializable
 {
     public int layer;
     public int width;
     public int height;
     public int[,] type;
     public int[,] bitmask;
     public int[,] status;
     public int[,] groupID;
     public void GetObjectData(SerializationInfo info, StreamingContext context)
     { 
       //omitted to save space
     }

     public TileMapData(SerializationInfo info, StreamingContext context)
     { 
        //omitted to save space
     }
 }

What would be a way to overcome this problem?

Second attempt using StreamWriter this time, still same problem.

      public void BinaryWriteSave(string saveLocation, object o)
     {
         byte[] byteArray = ObjectToByteArray(o);
         using (BinaryWriter bw = new BinaryWriter(File.Open(saveLocation, FileMode.Create)))
         {
             bw.Write(byteArray);
         }
     }


alt text

alt text

memoryerror1.png (151.9 kB)
hugememoryleak.png (14.9 kB)
Comment
Add comment · Show 3
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 rutter · Aug 14, 2014 at 10:00 PM 0
Share

What's the object?

Also, bear in $$anonymous$$d that memory allocated isn't necessarily memory used.

avatar image rutter · Aug 14, 2014 at 11:52 PM 0
Share

Without a reproducible example, this question is a moot point. You're excluding the exact information that would be necessary for useful discussion.

avatar image Stone-Legion · Aug 15, 2014 at 12:02 AM 0
Share

Added more code to try and give an example.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by smoggach · Aug 14, 2014 at 09:45 PM

It depends on your object. Unity tends to load assets into memory before serializing. I'm guessing your object depends on some uncompressed textures or you've run into a dependency issue.

Comment
Add comment · Show 2 · 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 Stone-Legion · Aug 14, 2014 at 11:32 PM 0
Share

No textures, I updated my post with more code.

avatar image smoggach · Aug 15, 2014 at 01:39 PM 0
Share

I think it's a dependency issue or due to the state of your object. I've heard some people run into a situation where unity thinks their object depends on every asset.

$$anonymous$$aybe some part or parts of your object needs to be uncompressed in memory before being serialized. This is the only reasons I can think of. I've only encountered this kind of behavior working with large amounts of textures.

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

23 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

Related Questions

Is this memory usage too much for a mobile game? 1 Answer

Unity not unloading procedurally generated mesh when parent GameObject is destroyed and its not referenced 0 Answers

Memory Allocation Continues to Grow? iOS. 4.6.3 0 Answers

Does the use of new VectorX within a loop cause a memory leak? 1 Answer

0 or Blank Ref Count Items in Memory Profiler 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