Why table does not have the __gc event?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Why table does not have the __gc event?

CHU Run-min
Just now, I found table does't have the __gc event, only userdata has.
But I don't know why.
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Ben Sunshine-Hill
On 1/15/06, CHU Run-min <[hidden email]> wrote:
> Just now, I found table does't have the __gc event, only userdata has.
> But I don't know why.
>

Basically, because it wouldn't do what you wanted it to do. Lua, like
many GCed languages, does not specify exactly when an unreferenced
object will actually be destroyed. It might be destroyed immediately;
it might be destroyed two months later. Because of this, destruction
is an unsuitable time to free scarce resources associated with an
object--closing windows, unlocking files, releasing printers, etc. If
you used __gc for this, you'd never know when a file would actually be
unlocked, so it would never be safe to try to use that file again. The
reason userdatas have __gc is so that they can free _memory_.

Bottom line: Incremental garbage collection makes finalizer methods
unsuitable for anything but freeing memory. If you need to free scarce
resources, do it manually.

Ben
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

CHU Run-min
Thanks, I understand it.
But lua-5.1 manual seems confusing me. It says

"Finalizers allow you to coordinate Lua's garbage collection with
external resource management (such as closing files, network or
database connections, or freeing your own memory). "
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Edwin Eyan Moragas-2
On 1/16/06, CHU Run-min <[hidden email]> wrote:
> Thanks, I understand it.
> But lua-5.1 manual seems confusing me. It says
>
> "Finalizers allow you to coordinate Lua's garbage collection with
> external resource management (such as closing files, network or
> database connections, or freeing your own memory). "
>

it simply means that whenever Lua collects garbage, it would also
call the __gc metamethods you have defined. this means you're also
leaving it up to the GC when it will clean up resources you have defined.

to mu understanding at least.

--
salud,
eyan

http://www.eyan.org
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Ben Sunshine-Hill
In reply to this post by CHU Run-min
On 1/15/06, CHU Run-min <[hidden email]> wrote:
> "Finalizers allow you to coordinate Lua's garbage collection with
> external resource management (such as closing files, network or
> database connections, or freeing your own memory). "
>

Yeah, I don't really like that wording. It really surprises me that it
isn't stated more clearly there what you can and can't expect from
__gc metamethods, and that __gc not working for tables isn't mentioned
at all (except by omission from discussions about __gc).

Ben
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Klaus Ripke
In reply to this post by CHU Run-min
On Mon, Jan 16, 2006 at 12:47:53PM +0800, CHU Run-min wrote:
> Thanks, I understand it.
> But lua-5.1 manual seems confusing me. It says
>
> "Finalizers allow you to coordinate Lua's garbage collection with
> external resource management (such as closing files, network or
> database connections, or freeing your own memory). "
For the most part this should be viewed as a desperate measure
of last resort only.

However, in some environments it might work out well.
Consider a process serving several requests sequentially,
each with it's own environment which is dropped afterwards
followed by a forced full gc.
You might get a behaviour similar to PHP,
i.e. lazy scripting works most of the time.

More generally it might be useful after catching an error,
which might mean some open ressources got lost on the way up.
Since programming in the try-catch-finally style on every call
level is slow, tedious and error prone, you might consider
doing a full gc after catching on some outer level.


regards
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Lisa Parratt
In reply to this post by CHU Run-min
CHU Run-min wrote:
> Just now, I found table does't have the __gc event, only userdata has.
> But I don't know why.

It's so those that don't understand the consequences of the garbage
collector don't get their fingers burnt, at the expense of usability for
those who do.

You could always patch the Lua source so __gc does get called for tables
- that's what I did.

--
Lisa
http://www.thecommune.org.uk/~lisa/
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Chris Marrin
In reply to this post by Klaus Ripke
Klaus Ripke wrote:

> On Mon, Jan 16, 2006 at 12:47:53PM +0800, CHU Run-min wrote:
>
>>Thanks, I understand it.
>>But lua-5.1 manual seems confusing me. It says
>>
>>"Finalizers allow you to coordinate Lua's garbage collection with
>>external resource management (such as closing files, network or
>>database connections, or freeing your own memory). "
>
> For the most part this should be viewed as a desperate measure
> of last resort only.

I'm not sure how desperate it is. The point of having __gc is to release
resources. For tables and strings, those resources are owned by Lua and
it therefore releases them without any intervention from you. Since
neither tables nor strings have any user controlled data, they have no
need for an externally accessible __gc method. If you want to put some
user controlled resources into a table, you should wrap them in a
userdata, which does have an __gc method. Then you get a chance to
release them. Because this method might not run for a long time, a
resource might be kept for longer than you expect, so you may want an
explicit destroy to control this. But the system is consistent. If you
implement __gc properly the resource will exist for as long as your
object exists, then it will cease to exist.

--
chris marrin              ,""$, "As a general rule,don't solve puzzles
[hidden email]        b`    $  that open portals to Hell" ,,.
         ,.`           ,b`    ,`                            , 1$'
      ,|`             mP    ,`                              :$$'     ,mm
    ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
   m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
  b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
  ```"```'"    `"`         `""`        ""`                          ,P`
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Chris Marrin
In reply to this post by Lisa Parratt
Lisa Parratt wrote:

> CHU Run-min wrote:
>
>> Just now, I found table does't have the __gc event, only userdata has.
>> But I don't know why.
>
>
> It's so those that don't understand the consequences of the garbage
> collector don't get their fingers burnt, at the expense of usability for
> those who do.
>
> You could always patch the Lua source so __gc does get called for tables
> - that's what I did.
>

But what resource could you possibly be getting rid of? Do you have some
lightuserdatas in the table that have some data? If so, then you should
make these full userdatas and attach __gc methods to them. I can't
imagine a case where a table needs an __gc method?

--
chris marrin              ,""$, "As a general rule,don't solve puzzles
[hidden email]        b`    $  that open portals to Hell" ,,.
         ,.`           ,b`    ,`                            , 1$'
      ,|`             mP    ,`                              :$$'     ,mm
    ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
   m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
  b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
  ```"```'"    `"`         `""`        ""`                          ,P`
Reply | Threaded
Open this post in threaded view
|

Re: Why table does not have the __gc event?

Lisa Parratt
Chris Marrin wrote:
> But what resource could you possibly be getting rid of? Do you have some
> lightuserdatas in the table that have some data? If so, then you should
> make these full userdatas and attach __gc methods to them. I can't
> imagine a case where a table needs an __gc method?

Well, if you really want to know, I'm doing low level rapid development
involving lots of memory manipulation. I don't have time to tweak
userdata and then wait tens of minutes for the project to rebuild.

As such I've plugged malloc and free into Lua, along with the
equivalents of peek and poke, in order to do the low level memory
manipulation. I implement my interfaces in Lua using these primitives,
meaning all I have to do to update the system is save the file.

One day, when the system is stable and fully featured, I might rewrite
the interfaces to use userdata, but not any time soon.

--
Lisa
http://www.thecommune.org.uk/~lisa/