Let's say we have a Ruby class like this:
假设我们有一个像这样的Ruby类:
class MyClass
def my_method(p)
# do some cool stuff with a huge amount of objects
my_objects = ...
return my_objects
end
end
And somewhere else in the application there's a function that calls MyClass
's my_method
, pretty much like this:
在应用程序的其他地方有一个调用MyClass的my_method的函数,非常类似:
def my_func
#doing some stuff ..
MyClass.my_method(some_param)
#doing other stuff ..
end
What happens to the list of objects
, is it eligible for garbage collection? Is it possible to know roughly when it's going to be collected?
对象列表会发生什么,是否有资格进行垃圾回收?是否有可能大致了解它何时被收集?
Is there a way to "mark" the list as eligible for GC? Maybe like this:
有没有办法将该列表“标记”为符合GC条件?也许是这样的:
def my_func
#doing some stuff ..
objects = MyClass.my_method(some_param)
objects = nil #does this make any difference?
#doing other stuff ..
end
2 个解决方案
#1
1
Items returned from the function are eligible for being collected once nothing else points to them.
从函数返回的项目一旦没有其他任何指向它们就有资格被收集。
So, if you ignore the return value and really nothing more remembers those objects, than yes, thay can be GC'ed.
所以,如果你忽略了返回值并且真的没有更多的记住那些对象,那么可以使用GC来实现这些对象。
So, if you store the result in objects
variable, then the returned values will be 'pinned'**) as long as the objects
variable still remembers them***). When you nil
that variable, they will be released and pending for collection. Nilling that variable may speed up their collection, but does not necessarily have to. *)
因此,如果将结果存储在objects变量中,那么返回的值将被“固定”**),只要对象变量仍然记住它们***)。当您将该变量设为零时,它们将被释放并等待收集。 Nilling这个变量可以加速他们的收集,但不一定非必要。 *)
UNLESS anything other still remembers them. If between the objects=f()
and objects=nil
you read the values from objects
variable and pass them to other functions/methods, and if they happen to store those objects, then of course it will pin them too, and "nilling" will help a bit in releasing the resources but not cause any immediate collection.*)
除了其他任何东西仍然记得他们。如果在objects = f()和objects = nil之间,你从objects变量中读取值并将它们传递给其他函数/方法,如果它们碰巧存储了那些对象,那么当然它也会将它们固定,并且“nilling”将有助于释放资源,但不会立即收集。*)
(*) In general, in environments with GC, you never actually know when the GC will run and what will it collect. You just know that objects that were forgotten by everyone will eventually be automatically removed. Nothing more. Theoreticaly, GC may choose to not run at all if your machine has terabytes of free memory.
(*)通常,在使用GC的环境中,您实际上从未知道GC何时运行以及它将收集什么。您只知道每个人都忘记的对象最终会被自动删除。而已。理论上,如果您的机器具有太字节的可用内存,GC可能会选择根本不运行。
(**) in some environments (like .Net) "pinning" is a precise term. Here I said it like that just to help you imagine how it works. I do not mean real pinning of memory blocks for communication with lower-level libraries, etc.
(**)在某些环境(如.Net)中,“固定”是一个精确的术语。在这里我说这就是为了帮助你想象它是如何工作的。我并不是指用于与较低级别库等通信的内存块的实际固定。
(***) When where's an object A remembers object B which remembers object C, and if the "B" becomes forgotten and if only B (and noone else) rememebers the C, then both B and C are GC'ed. So, you don't have to nil
the objects
. If the thing that contains objects
variable at some point becomes 'forgotten', then both the "outer thing", and "objects" and the "returned items" will be GC'ed. At least should be, if GC implementation is OK.
(***)当一个对象A在哪里记住对象B,它记住对象C,如果“B”被遗忘,如果只有B(和其他人)没有记住C,那么B和C都是GC。所以,你不必为了对象。如果在某个时刻包含对象变量的东西变得“遗忘”,那么“外部东西”,“对象”和“返回的项目”都将被GC。至少应该是,如果GC实施没问题。
This leaves one more thing to say: I do not say about GC in Ruby 2.0. All I've said was about garbage collectors in general. It applies also to Java, .Net, ObjC (with GC) and others. If you need to know precisely what happens in Ruby 2.0 and what are the gory details of GC implementation - ask directly about that :)
这还有一点要说:我没有在Ruby 2.0中说过GC。我所说的一切都是关于垃圾收集器的。它也适用于Java,.Net,ObjC(带GC)等。如果您需要准确了解Ruby 2.0中发生的事情以及GC实现的血腥细节,请直接询问:)
#2
2
GC destroys all objects which are not being referenced by your code. By setting objects to nil, you change reference of the variable, hence objects will be GCed, but exactly same thing is going to happen if you go with the first code. The real question is - why do you need for this object to be GB at precise moment - it shouldn't affect your code at all.
GC会破坏您的代码未引用的所有对象。通过将对象设置为nil,您可以更改变量的引用,因此对象将被GCed,但如果您使用第一个代码,则会发生完全相同的事情。真正的问题是 - 为什么你需要在精确的时刻将这个对象作为GB - 它根本不应该影响你的代码。
If you really want to have better control over garbage collection you can look at GC class: http://www.ruby-doc.org/core-1.9.3/GC.html. Note that you can rerun GC.start, which will force GC to run at that precise moment (even if there is nothing to collect).
如果您真的想要更好地控制垃圾收集,可以查看GC类:http://www.ruby-doc.org/core-1.9.3/GC.html。请注意,您可以重新运行GC.start,这将强制GC在该精确时刻运行(即使没有可收集的内容)。
#1
1
Items returned from the function are eligible for being collected once nothing else points to them.
从函数返回的项目一旦没有其他任何指向它们就有资格被收集。
So, if you ignore the return value and really nothing more remembers those objects, than yes, thay can be GC'ed.
所以,如果你忽略了返回值并且真的没有更多的记住那些对象,那么可以使用GC来实现这些对象。
So, if you store the result in objects
variable, then the returned values will be 'pinned'**) as long as the objects
variable still remembers them***). When you nil
that variable, they will be released and pending for collection. Nilling that variable may speed up their collection, but does not necessarily have to. *)
因此,如果将结果存储在objects变量中,那么返回的值将被“固定”**),只要对象变量仍然记住它们***)。当您将该变量设为零时,它们将被释放并等待收集。 Nilling这个变量可以加速他们的收集,但不一定非必要。 *)
UNLESS anything other still remembers them. If between the objects=f()
and objects=nil
you read the values from objects
variable and pass them to other functions/methods, and if they happen to store those objects, then of course it will pin them too, and "nilling" will help a bit in releasing the resources but not cause any immediate collection.*)
除了其他任何东西仍然记得他们。如果在objects = f()和objects = nil之间,你从objects变量中读取值并将它们传递给其他函数/方法,如果它们碰巧存储了那些对象,那么当然它也会将它们固定,并且“nilling”将有助于释放资源,但不会立即收集。*)
(*) In general, in environments with GC, you never actually know when the GC will run and what will it collect. You just know that objects that were forgotten by everyone will eventually be automatically removed. Nothing more. Theoreticaly, GC may choose to not run at all if your machine has terabytes of free memory.
(*)通常,在使用GC的环境中,您实际上从未知道GC何时运行以及它将收集什么。您只知道每个人都忘记的对象最终会被自动删除。而已。理论上,如果您的机器具有太字节的可用内存,GC可能会选择根本不运行。
(**) in some environments (like .Net) "pinning" is a precise term. Here I said it like that just to help you imagine how it works. I do not mean real pinning of memory blocks for communication with lower-level libraries, etc.
(**)在某些环境(如.Net)中,“固定”是一个精确的术语。在这里我说这就是为了帮助你想象它是如何工作的。我并不是指用于与较低级别库等通信的内存块的实际固定。
(***) When where's an object A remembers object B which remembers object C, and if the "B" becomes forgotten and if only B (and noone else) rememebers the C, then both B and C are GC'ed. So, you don't have to nil
the objects
. If the thing that contains objects
variable at some point becomes 'forgotten', then both the "outer thing", and "objects" and the "returned items" will be GC'ed. At least should be, if GC implementation is OK.
(***)当一个对象A在哪里记住对象B,它记住对象C,如果“B”被遗忘,如果只有B(和其他人)没有记住C,那么B和C都是GC。所以,你不必为了对象。如果在某个时刻包含对象变量的东西变得“遗忘”,那么“外部东西”,“对象”和“返回的项目”都将被GC。至少应该是,如果GC实施没问题。
This leaves one more thing to say: I do not say about GC in Ruby 2.0. All I've said was about garbage collectors in general. It applies also to Java, .Net, ObjC (with GC) and others. If you need to know precisely what happens in Ruby 2.0 and what are the gory details of GC implementation - ask directly about that :)
这还有一点要说:我没有在Ruby 2.0中说过GC。我所说的一切都是关于垃圾收集器的。它也适用于Java,.Net,ObjC(带GC)等。如果您需要准确了解Ruby 2.0中发生的事情以及GC实现的血腥细节,请直接询问:)
#2
2
GC destroys all objects which are not being referenced by your code. By setting objects to nil, you change reference of the variable, hence objects will be GCed, but exactly same thing is going to happen if you go with the first code. The real question is - why do you need for this object to be GB at precise moment - it shouldn't affect your code at all.
GC会破坏您的代码未引用的所有对象。通过将对象设置为nil,您可以更改变量的引用,因此对象将被GCed,但如果您使用第一个代码,则会发生完全相同的事情。真正的问题是 - 为什么你需要在精确的时刻将这个对象作为GB - 它根本不应该影响你的代码。
If you really want to have better control over garbage collection you can look at GC class: http://www.ruby-doc.org/core-1.9.3/GC.html. Note that you can rerun GC.start, which will force GC to run at that precise moment (even if there is nothing to collect).
如果您真的想要更好地控制垃圾收集,可以查看GC类:http://www.ruby-doc.org/core-1.9.3/GC.html。请注意,您可以重新运行GC.start,这将强制GC在该精确时刻运行(即使没有可收集的内容)。