I'm not sure I understand scope - does an out-of-scope variable (I'm using Ruby) exist in memory somewhere or does it stop existing (I know you can't access it). Would it be inaccurate to say that an out-of-scope variable does not exist any more?
我不确定我是否理解范围 - 一个超出范围的变量(我正在使用Ruby)存在于某个地方的内存中,或者它是否停止存在(我知道你无法访问它)。说超出范围的变量不再存在是否不准确?
Maybe this is a philosophical question.
也许这是一个哲学问题。
7 个解决方案
#1
5
If you are using managed language then you don't allocate and unallocate memory so as far as you are concerned it no longer exists.
如果您使用托管语言,那么您不会分配和取消分配内存,因此就您而言它不再存在。
Technically it does but GCs tend not to be deterministic so technically it's hard to say when it actually vanishes.
从技术上来说它确实如此,但是GC往往不具有确定性,所以从技术上来说很难说它何时会消失。
#2
3
A variable is not the same as the value it holds.
变量与其保存的值不同。
The variable itself ceases to exist when it goes out of scope. The value that the variable held may represent an object, and that object may continue to exist beyond the lifetime of the variable. The garbage collector reclaims the object later.
当变量超出范围时,变量本身就不再存在。变量保持的值可以表示对象,并且该对象可以在变量的生命周期之外继续存在。垃圾收集器稍后回收该对象。
#3
2
When it goes out of scope it still exists (in the sense that it has some memory allocated to it) for some time, until garbage collection cleans it up. But as you imply, it's lost it's name and is unreachable.
当它超出范围时,它仍然存在(在某种意义上它有一些内存分配给它)一段时间,直到垃圾收集清理它。但正如你暗示的那样,它失去了它的名字并且无法到达。
#4
2
When a variable falls out of scope is anyone around to hear it scream?
当变量超出范围时,周围的任何人都会听到它尖叫吗?
This isn't a ruby question so much as a general question about garbage collection. In a garbage collected language such as Ruby or C# when a variable falls out of scope it's marked in some manner that says it's no longer in use. When this happens you can't get at it any more and it sits around twiddling its thumbs - but it does still have memory allocated to it.
这不是一个红宝石问题,而是关于垃圾收集的一般问题。在垃圾收集语言(如Ruby或C#)中,当变量超出范围时,它会以某种方式标记,表明它已不再使用。当发生这种情况时,你不能再得到它并且它绕着它的拇指摆动 - 但它仍然有分配给它的记忆。
At some point the garbage collector will wake up and look for variables marked as not in use. It will dispose of them and at that point they're no longer in memory at all.
在某些时候,垃圾收集器将唤醒并查找标记为未使用的变量。它将处置它们,此时它们根本就不再存在。
It can be more complicated than this, depending on how the garbage collector works, but it's close enough :)
它可能比这更复杂,取决于垃圾收集器的工作方式,但它足够接近:)
#5
1
It exists for a little bit until the garbage collector disposes it (if it can).
它存在一点点,直到垃圾收集器处理它(如果它可以)。
#6
1
Rob Kennedy has this answered appropriately, but I thought I would add a little more detail.
Rob Kennedy对此做出了适当的回答,但我想我会添加一些细节。
The important thing to recognize is the difference between a variable and the value it represents.
要认识到的重要一点是变量与它所代表的值之间的差异。
Here's an example (in C# because I don't know Ruby):
这是一个例子(在C#中,因为我不知道Ruby):
object c = null;
if (1 == 1) // Just to get a different scope
{
var newObj = new SomeClass();
newObj.SomeProperty = true;
c = newObj;
}
In the code above, newObj goes out of scope at the end of the if statement and as such "doesn't exist", but the value that it was referring to is still alive and well, referenced by c. Once all of the references to the object are gone, then the garbage collector will take care of cleaning it up.
在上面的代码中,newObj超出了if语句末尾的范围,因此“不存在”,但它引用的值仍然存活且很好,由c引用。一旦对象的所有引用都消失了,那么垃圾收集器将负责清理它。
#7
0
If you're talking about file objects, it becomes more than a philosophical question. If I recall correctly, files do not close automatically when they go out of scope - they only close if you ask them to close, or if you use a File.open do |file|
style block, or if they get garbage collected. This can be an issue if other code (or unit tests) try to read the contents of that file and it hasn't yet been flushed.
如果你在谈论文件对象,它不仅仅是一个哲学问题。如果我没记错的话,文件在超出范围时不会自动关闭 - 只有在你要求它们关闭时才关闭,或者你使用File.open do | file |样式块,或者如果他们收集垃圾。如果其他代码(或单元测试)尝试读取该文件的内容并且尚未刷新,则可能会出现问题。
#1
5
If you are using managed language then you don't allocate and unallocate memory so as far as you are concerned it no longer exists.
如果您使用托管语言,那么您不会分配和取消分配内存,因此就您而言它不再存在。
Technically it does but GCs tend not to be deterministic so technically it's hard to say when it actually vanishes.
从技术上来说它确实如此,但是GC往往不具有确定性,所以从技术上来说很难说它何时会消失。
#2
3
A variable is not the same as the value it holds.
变量与其保存的值不同。
The variable itself ceases to exist when it goes out of scope. The value that the variable held may represent an object, and that object may continue to exist beyond the lifetime of the variable. The garbage collector reclaims the object later.
当变量超出范围时,变量本身就不再存在。变量保持的值可以表示对象,并且该对象可以在变量的生命周期之外继续存在。垃圾收集器稍后回收该对象。
#3
2
When it goes out of scope it still exists (in the sense that it has some memory allocated to it) for some time, until garbage collection cleans it up. But as you imply, it's lost it's name and is unreachable.
当它超出范围时,它仍然存在(在某种意义上它有一些内存分配给它)一段时间,直到垃圾收集清理它。但正如你暗示的那样,它失去了它的名字并且无法到达。
#4
2
When a variable falls out of scope is anyone around to hear it scream?
当变量超出范围时,周围的任何人都会听到它尖叫吗?
This isn't a ruby question so much as a general question about garbage collection. In a garbage collected language such as Ruby or C# when a variable falls out of scope it's marked in some manner that says it's no longer in use. When this happens you can't get at it any more and it sits around twiddling its thumbs - but it does still have memory allocated to it.
这不是一个红宝石问题,而是关于垃圾收集的一般问题。在垃圾收集语言(如Ruby或C#)中,当变量超出范围时,它会以某种方式标记,表明它已不再使用。当发生这种情况时,你不能再得到它并且它绕着它的拇指摆动 - 但它仍然有分配给它的记忆。
At some point the garbage collector will wake up and look for variables marked as not in use. It will dispose of them and at that point they're no longer in memory at all.
在某些时候,垃圾收集器将唤醒并查找标记为未使用的变量。它将处置它们,此时它们根本就不再存在。
It can be more complicated than this, depending on how the garbage collector works, but it's close enough :)
它可能比这更复杂,取决于垃圾收集器的工作方式,但它足够接近:)
#5
1
It exists for a little bit until the garbage collector disposes it (if it can).
它存在一点点,直到垃圾收集器处理它(如果它可以)。
#6
1
Rob Kennedy has this answered appropriately, but I thought I would add a little more detail.
Rob Kennedy对此做出了适当的回答,但我想我会添加一些细节。
The important thing to recognize is the difference between a variable and the value it represents.
要认识到的重要一点是变量与它所代表的值之间的差异。
Here's an example (in C# because I don't know Ruby):
这是一个例子(在C#中,因为我不知道Ruby):
object c = null;
if (1 == 1) // Just to get a different scope
{
var newObj = new SomeClass();
newObj.SomeProperty = true;
c = newObj;
}
In the code above, newObj goes out of scope at the end of the if statement and as such "doesn't exist", but the value that it was referring to is still alive and well, referenced by c. Once all of the references to the object are gone, then the garbage collector will take care of cleaning it up.
在上面的代码中,newObj超出了if语句末尾的范围,因此“不存在”,但它引用的值仍然存活且很好,由c引用。一旦对象的所有引用都消失了,那么垃圾收集器将负责清理它。
#7
0
If you're talking about file objects, it becomes more than a philosophical question. If I recall correctly, files do not close automatically when they go out of scope - they only close if you ask them to close, or if you use a File.open do |file|
style block, or if they get garbage collected. This can be an issue if other code (or unit tests) try to read the contents of that file and it hasn't yet been flushed.
如果你在谈论文件对象,它不仅仅是一个哲学问题。如果我没记错的话,文件在超出范围时不会自动关闭 - 只有在你要求它们关闭时才关闭,或者你使用File.open do | file |样式块,或者如果他们收集垃圾。如果其他代码(或单元测试)尝试读取该文件的内容并且尚未刷新,则可能会出现问题。