-1

A 16kb file deserialized first time allocate about 3.6M memory~~ and the second ~only allocate 50kb memory,I know it cache the reflection infos , But How could I realse the memory by manual?

I want to know how to control the GC used in Unity3d, help~~~

First:

Second:

Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
  • Related or duplicate: [Does Json.NET cache types' serialization information?](https://stackoverflow.com/a/33558665/3744182). – dbc Dec 10 '18 at 23:45

1 Answers1

0

Unity uses Automatic Memory Management. In most cases, you don't need to manually collect garbage.

You should call GC.Collect only when you are absolutely sure it's the "right" time. You definitely don't want this process to freeze your game character.

To quote Unity on this topic:

If we know that heap memory has been allocated but is no longer used (for example, if our code has generated garbage when loading assets) and we know that a garbage collection freeze won’t affect the player (for example, while the loading screen is still showing), we can request garbage collection

You can read more on this Unity Page.

bolkay
  • 1,881
  • 9
  • 20
  • thanks for anwer, but can i reduce the allocation on first time? 3.6 m allocate is too much,it just a 16kb file... – talent_jian Dec 10 '18 at 16:15
  • I fear you can't do much. The JSON parser has to completely read the data before it can access the values therein. You can only manually collect it when done. That's the point. – bolkay Dec 10 '18 at 16:24