12-28-2008, 08:05 AM
Ah yes, I see Wolfie. That is indeed misleading.
What they failed to mention is how much memory is actually used. Totally insignificant except in the most extreme cases which certainly never have happened in RMXP or RMVX unless caused by a bug. (Not including specifically designed tests)
They may have been thinking about how the referenced object, which can be of a significant size, may be present in the memory all the time since the user just used a global variable rather than a local variable.
At this point I think I should remind you that when the scope of a local variable ends the local variable is freed, but the object it references is NOT freed. For freeing objects you have the Garbage Collector (GC) which frees objects you have no reference to (under transitive closure). If the local variable was the only reference to the object then the object will freed whenever the GC gets to it. You can't really know when the GC runs unless you force start it with GC.start. (It is generally preferable to let the GC manage itself)
What now if you have a global variable and want the object it points to freed? Just do something like $global_variable = nil. Actually it can be anything other than the current object.
If there are no more references to the object then the GC will take of it.
The rest of what Tricky wrote is pretty much true. (Note: I think than on a 1-to-1 ratio local variables might use more memory than global variables since they are in a dynamic scope as opposed to a static scope.)
Global variables are NOT resource hogs unless you consider humans as a resource. Then it might be so.
For the CPU global variables are insignificant.
*hugs*
- Zeriab
Edit:
@Charlie: The idea is that global variables don't disappear after introduced so they keep using memory where as local variables are disposed and therefore the number of variables would generally be lower.
For the scope of an RPG Maker XP or VX game I really believe the difference to be insignificant. Maybe it's only for 99 % though.
What they failed to mention is how much memory is actually used. Totally insignificant except in the most extreme cases which certainly never have happened in RMXP or RMVX unless caused by a bug. (Not including specifically designed tests)
They may have been thinking about how the referenced object, which can be of a significant size, may be present in the memory all the time since the user just used a global variable rather than a local variable.
At this point I think I should remind you that when the scope of a local variable ends the local variable is freed, but the object it references is NOT freed. For freeing objects you have the Garbage Collector (GC) which frees objects you have no reference to (under transitive closure). If the local variable was the only reference to the object then the object will freed whenever the GC gets to it. You can't really know when the GC runs unless you force start it with GC.start. (It is generally preferable to let the GC manage itself)
What now if you have a global variable and want the object it points to freed? Just do something like $global_variable = nil. Actually it can be anything other than the current object.
If there are no more references to the object then the GC will take of it.
The rest of what Tricky wrote is pretty much true. (Note: I think than on a 1-to-1 ratio local variables might use more memory than global variables since they are in a dynamic scope as opposed to a static scope.)
Global variables are NOT resource hogs unless you consider humans as a resource. Then it might be so.
For the CPU global variables are insignificant.
*hugs*
- Zeriab
Edit:
@Charlie: The idea is that global variables don't disappear after introduced so they keep using memory where as local variables are disposed and therefore the number of variables would generally be lower.
For the scope of an RPG Maker XP or VX game I really believe the difference to be insignificant. Maybe it's only for 99 % though.