GC in lambdas?

A

ako...

hello,

someone said in another conversation:
also note that lambda will prevent __everything__ in it's scope from __ever__
being garbage collected.

could someone please confirm this? i am also interested to know why GC
is so different in lambdas and whether the objects will be collected
when lambda itself goes out of scope?

thanks
konstantin
 
E

Edward Faulkner

--hHWLQfXTYDoKhP50
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=20
could someone please confirm this? i am also interested to know why GC
is so different in lambdas and whether the objects will be collected
when lambda itself goes out of scope?

There's nothing different about GC within lambdas. You simply need to
be aware of how lambdas work.

The lambda object needs to maintain references to everything that was
visible within the scope where it was created. As long as the lambda
object is alive, everything it references must also stay alive.

If you allow the lambda object to be garbage-collected, the things it
references will also be subject to garbage collection. =20

--hHWLQfXTYDoKhP50
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDncf8nhUz11p9MSARAs8qAKCZWpA+AqsllQGT7dBCddWa3V7vBgCePj2w
hjDG4AkHvAf1OMFHv8iG9Ac=
=lyGU
-----END PGP SIGNATURE-----

--hHWLQfXTYDoKhP50--
 
A

ako...

thank you. if lambda creates an object during its execution, and by the
time lambda finishes the object goes out of scope, is the object
garbage collected?
 
E

Eric Hodel

thank you. if lambda creates an object during its execution, and by
the
time lambda finishes the object goes out of scope, is the object
garbage collected?

The object is eligible for garbage collection. It may or may not be
collected immediately.

def inner
obj = Object.new # <-- object created
return lambda {} # <-- object held by lambda, not eligible for GC
end

def outer
my_proc = inner # <-- object still held by lambda, not eligible
for GC
return nil
end

outer # <-- proc eligible for GC, so created object eligible for GC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top