Forum Groups
  All forums
    Help & Feedback
      Questions
      Work in progress
      Finished Art
      Non-Max related
    Community
      Offtopic
      News
    Hardware
    Photography



Maxunderground news unavailable

programming - object falling "out of scope"
show user profile  digs
I code in c# for unity and I've been researching memory leaks. I see in a lot of forums that a cause of memory leaking can occur when an object "falls out of scope" and I'm wondering what that means exactly

read 90 times
11/28/2016 7:15:13 PM (last edit: 11/28/2016 7:15:13 PM)
show user profile  LionDebt
I'll take a punt:

Scope; is basically a defined area of your code. Everything inside it knows about everything else.

In the simplest sense, curly-brackets { } will be one chunk of code, everything inside it knows about the variables present, the methods / functions, parameters etc.

The Scope of a Variable however, is defined through it's accessibility.

Public variables are visible anywhere in your project. Make a class called Foo, declare a Public Bar variable, and from any script you can now access Foo.Bar and play with that variable.

Private vars are only visible within the block / scope where they are defined.

Protected vars can only be used in the class (scope) where they are defined, or within any inherited class.

Just realized you probably know all of this already.

AFAIK garbage collection within Unity is pretty spot on and automatic.

The memory manager tracks areas in the heap that it knows are unused. When you instantiate a new object (assign a block of memory), it chooses an empty area to put this block and removes the memory that was allocated from the heap (that it knows is free / unused). All requests (object instantiation in this example) are handled the same until there is not enough memory (or space available) to allocate. It's pretty unlikely at this point that "all the memory" known to be in the heap is actively still being used. If all references to an object (memory block) are gone then the memory that stores the object can be reallocated / or set to 'free/unused' again.

In plainer English and how I understand it at least: Keep your variables confined to the 'scope' / method / function they are in. If you need to run a quick method to adjust some values on an object, pass the object as a parameter into your method, along with the values to change, do all the 'changes' and then once that method is finished running - there will be no references to the values (and the memory will be free'd up).

Someone else might be able to explain this more concisely / technically.

But essentially so long as you are not going wild with public variables for everything, keeping your references / object structures tidy, passing parameters and using methods: like getters; setters (accessor methods) to define / access private variables when needed... Tl;dr encapsulation. You should be alright.






read 74 times
11/29/2016 12:26:50 AM (last edit: 11/29/2016 12:31:44 AM)
show user profile  digs
thanks for the feedback ld

I'm wondering if there's a pretty straight forward example of when an object falls out of scope. (I understand the difference between local/global scope, but just now realize that i " " quoted the title x), sry) I'm reading that this means the program has lost a reference to the object that was created. I don't think this has happened in my game, but am interested to see an example of code in which an object falls out of scope thus causing a memory leak so I know for sure/know what to avoid.

Will google some more later, at work currently :)
read 53 times
11/29/2016 10:55:08 PM (last edit: 11/29/2016 11:49:36 PM)
show user profile  spoon
You need a memory profiler. Clr profiler is free but hard to interpret.


read 34 times
11/30/2016 4:29:19 PM (last edit: 11/30/2016 4:29:19 PM)
show user profile  spoon
If you wan to know more about this subject, check the books by Jeffrey Richter on the CLR.


read 21 times
12/1/2016 12:45:05 PM (last edit: 12/1/2016 12:45:05 PM)
#Maxforums IRC
Open chat window


Support Maxforums.org