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 sdgd · Oct 26, 2013 at 08:59 PM · c#variablesscopegarbage collector

Does Garbage Collector Collect variables Made Inside Scope?

Since my statement in this Question is wrong I'm asking here to learn more about that

here at last answer says

myMethodVariable is already marked for garbage collection when it Start() is completed.

but this script is a little bit different

so it would mean that variable is marked for garbage collection when SomeFunction() exits

as far as I still understand means Garbage collector still needs to find and collect 1000 variables

 void Start(){
     for (int i=0; i<1000; i++) {
         SomeFunction();
     }
 }
 void SomeFunction(){
     float Variable = 4;
     // some code to use variable
     if (Variable == 4){
     Variable = 5;
     }
 }

but as @robertbu says that Start() and function() are different things, ...

does that mean?

IF in Start() after variable exits it will be marked for collect

but if in Function() it will be freed on the spot?

Comment
Add comment · Show 2
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 robertbu · Oct 26, 2013 at 09:14 PM 0
Share

Garbage collection only collects things that are allocated out of the heap. Your variables because they are by value, are declared on the stack.

avatar image robertbu · Oct 26, 2013 at 09:20 PM 1
Share

I may have mislead you here. If a variable is declared on the heap (like strings are), then when there are no longer references to that string it is freed. If the variable is declared inside of a function, then usually it passes out of scope (and therefore no longer has any references) when you return from a function. It is possible to preserve references to that variable (assu$$anonymous$$g it is not a value type), by passing that reference to some other place in the code. As long as something is referencing the variable, the garbage collector will not reclaim the memory.

1 Reply

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

Answer by Hoeloe · Oct 26, 2013 at 09:11 PM

The variable will not be visible outside the scope of SomeFunction. To understand how the garbage collector works, you need to understand a little about the Stack and the Heap.

The Stack is a bit like the "working frame" of the program. It "stacks up" local data, and executes code. Value type variables are stored on the stack, and reference types store pointers into the Heap. When a function enters, it gets the local "frame" pushed onto the top of the Stack, and when it exits, all the local data is popped off again. This means that value-type variables are instantly deleted the moment the function exits scope, and pointers to reference types are also instantly deleted.

The Heap is a little more complicated. In short, it stores data anywhere it can, and the program relies on being told where that data is (that's where the pointers come in). The garbage collector only works on the Heap. It does nothing to the Stack, except use it in determining what is "garbage". In short, it determines that garbage is whatever cannot be reached from the Stack, so it follows all the pointers in the Stack, recursively following through, marking everything it reaches. Then it sweeps over the Heap, deleting everything that is not marked. This means that if there is no pointer to an object on the Heap that can be reached from the Stack, it is deleted at the next garbage collect.

Now, remember that the Stack stores the current pointers to objects in the Heap, and that it deletes these pointers when the function exits. So, if a local object is created, it puts the object in the Heap, but the only pointer to it exists in the function's stack frame. This means that when the function exits, the only reference to the object is deleted. This in turn means that the object will not be reached during the next "mark" phase of the garbage collector, and so will be deleted.

Hopefully, this has helped a little in understanding how the garbage collector operates.

EDIT:

I'm not sure I made this quite clear, but I want to point out that as long as there is a reference to an object that is still in scope, then it will not be garbage collected.

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 sdgd · Oct 26, 2013 at 09:29 PM 0
Share

thanks you actually answered this Q and the other one as well

and I learned for the other Q even if I'll mark stuff $$anonymous$$eepAlive it'll still have to sweep through whole stack and heap to see what to keep

meaning performance will not be better

avatar image Hoeloe · Oct 26, 2013 at 09:35 PM 1
Share

You can't boost performance like that. What you can do, though, is control when you run the garbage collector (using System.GC.Collect). By using this at sensible points (for example, between having loaded a scene and displaying it), you can reduce the work the garbage collector has to do the next time, while ensuring memory isn't wasted. It's much more difficult to get right, though.

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

16 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

Related Questions

why does my object reference go out of scope? 1 Answer

Class Scope Variable 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Increase difficulty Space Shooter 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