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
1
Question by DaveLloyd · May 31, 2012 at 03:11 PM · assetsresourcesanimationclipgarbage collection

How do resources such as AnimationClips get garbage collected?

I have a GameObject with an Animation component attached to it. I load some AnimationClips into it (via Resources.Load). I then Destroy this GameObject and create a new one. Similarly I attach an Animation component to it and load some AnimationClips.

I was expecting this sequence to have constant memory bounds but in fact each time the GO is destroyed and recreated I get another 10Mb added to the AnimationClips (according to Unity's profiler) and shortly afterwards my app is killed by iOS after a memory warning.

What must I do to force the memory from the AnimationClips to be reclaimed? I had assumed that destroying the GO would do the job (or the Animation component). I've also tried caching the clips and destroying them explicitly but Unity prevents that due to "possible data loss". As far as I can tell I have no other references to the original GO or the clips that would prevent the GC from collecting them.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by whydoidoit · May 31, 2012 at 04:33 PM

You should probably try calling Resources.UnloadUnusedAssets http://unity3d.com/support/documentation/ScriptReference/Resources.UnloadUnusedAssets.html

Comment
Add comment · Show 5 · 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 flamy · May 31, 2012 at 04:37 PM 1
Share

ya that is right. and additional info it actually have a jerk when called, so dont keep calling each frame.

avatar image DaveLloyd · Jun 01, 2012 at 10:15 AM 0
Share

That did it! I put Resources.UnloadUnusedAssets() in an OnDestroy() method of the relevant GO and memory usage is now more or less stable. As flamy noted it does stall a little so I'll have to hide that behind a bit of UI. $$anonymous$$any thanks!

avatar image whydoidoit · Jun 01, 2012 at 10:24 AM 0
Share

Glad to help! Can you mark the answer as accepted to get it off the list?

avatar image DaveLloyd · Jun 01, 2012 at 10:41 AM 0
Share

Gotcha! (New to this Q&A format)

avatar image whydoidoit · Jun 01, 2012 at 10:43 AM 0
Share

Catches everyone out - yours truly included :)

avatar image
1

Answer by Bunny83 · May 31, 2012 at 04:47 PM

Resources itself aren't duplicated when using Resources.Load as far as i know. If you call Resources.Load on the same resource, it will return the same instance. The first time you use Load it will load the asset into memory. So the real assets aren't duplicated.

However some assets are duplicated by the using component. For example a material that is assigned to the .material property of a renderer will create a duplicate and use that. When using sharedMaterial, no duplicates are generated.

Again, as far as i know AnimationClips are also shared between Animation components, but AnimationStates aren't. Usually they should be collected after the owning component has been destroyed.

If you can reproduce the issue, create a small test project and file a bug report.

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 DaveLloyd · Jun 01, 2012 at 10:19 AM 0
Share

I did a bit more experimenting before finding out about Resources.UnloadUnusedAssets and I think what you're say would explain something odd I saw: I put in a cache in front of Resources.Load and was still seeing the memory ramp up but then I removed the AddClip from the Animation component and memory use was suddenly stable. It looks like AnimationClips are duplicated for the AnimationState in each Animation component.

I'll see if I can put together a repro because that would seem like a bug: a separate instance of each AnimationClip (already the bulkiest of my assets) for each animating character would be very heavy!

avatar image Bunny83 · Jun 01, 2012 at 04:51 PM 0
Share

I don't think they duplicate the clips, but haven't done any researches in that direction ;) $$anonymous$$aybe they duplicate them since you can create / change clips at runtime, but not sure about that.

Anyways, when the component is destroyed those duplicates should be removed as well. If they stay it's a bug / memleak in Unity.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity not allowing drag and drop for assets in Project View 2 Answers

Loading a ScriptableObject at Runtime. 1 Answer

Is there an alternative to AssetBundles? 1 Answer

AssetGraph bug fixed request(Override Import Setting doesn't work) 0 Answers

How to load an asset without a reference? 2 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