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 wangunity · Jan 06, 2015 at 10:25 AM · memorygc

Does local reference variable memory free as method finish execution ?

as we all know, if a method finish execution, all the local variables memory will be free. so i have a question , if i new a object ,what will happen if the method finish execution ? my code is below.

     /// <summary>
     /// Free the memory of the new object.
     /// </summary>
     void FreeMemory()
     {
         Object temp = new Object();
     }


i think the temp 's memory will not be free until GC collect it. because the temp object is in the heap not stack.

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 Mexallon · Jan 06, 2015 at 10:55 AM 1
Share

If the $$anonymous$$ethod should work properly and is not just for testing purpose it could take a parameter (Object for example) and just set it to null. In C++ you could use delete but as you correctly stated above the GC is in duty to collect unused memory.

avatar image wangunity · Jan 07, 2015 at 01:56 AM 0
Share

thank you!

avatar image wangunity · Jan 07, 2015 at 03:38 AM 0
Share

as the top code, like you say the temp object 's reference value will be null after the method exit. so i should not assign temp=null before method exit ? just automatic free by stack.

1 Reply

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

Answer by HarshadK · Jan 06, 2015 at 10:31 AM

The pointer to the Object will no longer be available but the memory allocated to the object will still stay allocated until next garbage collection.

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 wangunity · Jan 07, 2015 at 01:56 AM 0
Share

thank you!

avatar image wangunity · Jan 07, 2015 at 03:40 AM 0
Share

the Object will no longer be available , i can say the temp =null ? so i don't need reassign temp = null after the method exit with the above example code.

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

26 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

Related Questions

Textrue2D is not cleaned after Apply(false,true) 0 Answers

Questions About Allocations - Arrays - Structs and so on 1 Answer

Do any variables declared within methods generate memory allocation? 1 Answer

How can code generate memory garbage without instantiating any memory? 4 Answers

Asset is not unloaded even if GameObject destroyed 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