lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I think this really comes down to two cases:

1. You know the lifetime for the object and you can explicitly close it when done -- e.g., by calling f:close(). f:close() is much clearer that the goal is to close the resource. f = nil just means "I don't care about this particular reference to the resource anymore".

2. You don't know the lifetime. Then you are looking to the system to find all of the potentially live references and the only way to do that with absolute certainty is to complete a full GC. Reference counting won't get it for you because of cycles. Now, maybe the objects you are interested in -- e.g., file handles -- can't be directly involved in cycles, but the objects referencing them -- e.g., tables -- could be.

One could patch the Lua VM so that assignments adjusted reference counts and this would result in some items getting collected faster, but it would not ensure that all objects would get finalized the moment they became unreachable because that's a problem that requires global rather than local information.

This is not unique to Lua. The languages that seem to give one immediate collection do so by restricting the data structures one can build or by expecting the programmer to restrict the choice of data structures and/or how they are used.