This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: destroy and garbage collection


* teong leong chuah [2010-05-27 11:07+0200] writes:

> Hi Per,
>
> Can you explain how objects in kawa are destroyed or set as
> unreferenced and waiting for garbage collection?

The rules for Kawa are the same as for for the JVM: all reachable
objects, starting from local variables (on the stacks of running
threads) and static variables, are not collected.  (define foo ..) at
toplevel usually creates a static variable.

> In my code I am using objects extensively in the form of lambda
> objects with some local data
>
> Example:
>
> (define (my-object)
>  (let ((data1 'some-data)
>          (data2 'some-data2))
>
>    (lambda (message)
>       (cond ((equal? message 'set-data1)
>                  (lambda (self new-value)
>                       (set! data1 new-value)))))
>
> Something like the above. We've got a procedure that will interact
> with the lambda object inside and retrieve the correct procedure based
> on what message is passed in and applying that procedure to a some
> arguments.

The free variables in the body of a lambda, i.e. the variables which
aren't arguments, can potentially be referenced and must be kept alive
if the lambda is reachable.

> This works in general but what I haven't figure out is how to destroy
> objects in kawa. 

There's no other way to "destroy" an object other than not referencing
it.

> We would typically have many such objects store in a
> scheme list or a hashtable and we've been doing (set! mylist '()) or
> empty the hashtable.
>
> Does that ensure the deletion of such objects that were previously
> inside the list?

Only if the list/hashtable is the last object with a reference to the
objects inside.  If there's another way to reach the elements they can't
be freed.

> We have our own thread object that is created using a loop, a sleep
> and future to create a periodically firing thread that execute a
> procedure. We've put threads inside our objects and it seems with, the
> thread still firing and running, the objects that have been emptied
> from the list are still alive. 

Threads and the objects referenced by the thread can only be freed after
the thread terminates.

> If I stop the thread there wouldn't be
> any way of knowing whether my objects are alive or not.

There are some heap inspecting tools like jhat or hprof but those are 
hard to use.

> Can you walk me through the scheme way of destroying objects?

Dropping references to objects by setting variables to nil and clearing
hashtables is the right approach.  And watch out for free variables in
lambdas.

Helmut


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]