• 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 ungalyant2 · Aug 14, 2013 at 10:53 AM · listsclassesmemory-leak

Cleaning Up Classes In Lists

I've had a concern for a w$$anonymous$$le, and from searc$$anonymous$$ng around I haven't been able to find a definite answer. When making a list of any given class, by simply removing that class from the list, wit$$anonymous$$n Unity, does that inherently remove the class from memory, or are there further steps that need to be taken to make sure t$$anonymous$$s happens? Normally such a t$$anonymous$$ng would lead to a memory leak, but I haven't yet had t$$anonymous$$s problem with Unity (from what I can tell) but it makes me a little cautious when I don't know.

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
0

Answer by fafase · Aug 14, 2013 at 11:02 AM

Removing an item from a list only remove the reference to the item contained in the list. A list does not hold the object but just a reference to it.

As a result, when doing:

 list.RemoveAt(index);

the object is still in memory and could still be retrieved with GameObject.Find("Name").

T$$anonymous$$s below will remove the object on collision:

 void OnCollisionEnter(Collision col)
 {
    if(col.gameObject.tag == "Projectile"){
       projectileList.Remove(col.gameObject);
       Destroy(col.gameObject);
    }
 }

That considers you are storing your projectile in a list right after you instantiate them. T$$anonymous$$s will remove from the list and destroy the object. Later on garbage collector will free the memory when needed.

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 ungalyant2 · Aug 14, 2013 at 11:05 AM 0
Share

I don't specifically mean game objects that are in the heirarchy, I mean a list of classes that is held purely as information. Not game objects. Is the rule the same?

avatar image fafase · Aug 14, 2013 at 11:17 AM 1
Share

oh you mean removing a script from an object. That cancels the creation of the instance of the object yes.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Add Item To A List Of Classes 0 Answers

Lists within lists to make a crafting system 1 Answer

How to carry List's and Vector3's to other classes? Interfaces don't work? 4 Answers

Access list storing custom class variables from another script 1 Answer

How to access individual class elements, that has an array, that is in a list for c# 1 Answer


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