Scala object destructors?

4,201 views
Skip to first unread message

Jean Yang

unread,
Mar 15, 2013, 11:18:48 PM3/15/13
to scala-user
Hello,

  I was wondering if Scala objects had associated destructors, or whether there is some way standard way of simulating this, since from a brief online search it seems that Scala objects don't...

Thanks,
Jean

--
Jean Yang
website | twitter

Shrivats

unread,
Mar 15, 2013, 11:29:09 PM3/15/13
to Jean Yang, scala-user
On Sat, Mar 16, 2013 at 8:48 AM, Jean Yang <jean...@csail.mit.edu> wrote:
> Hello,
>
> I was wondering if Scala objects had associated destructors, or whether
> there is some way standard way of simulating this, since from a brief online
> search it seems that Scala objects don't...

Hi,

AFAIK, Scala doesn't have destructors (or even finalizers like in
Java). However, if you're doing this in the context of IO, you might
find this SO post helpful, particularly the Closeable interface, which
you can implement -
http://stackoverflow.com/questions/4002343/how-to-write-a-class-destructor-in-scala

Also, have a look at: https://github.com/jsuereth/scala-arm

Shrivats

wohlgemuth

unread,
Mar 15, 2013, 11:29:08 PM3/15/13
to Jean Yang, scala-user
i would guess that finalize should work as it does i java.


g.


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
------------------------------------------------------------
Lead Developer - Fiehnlab, UC Davis

gert wohlgemuth

work:
http://fiehnlab.ucdavis.edu/staff/wohlgemuth

phone:
530 665 9477

coding blog:
http://codingandmore.blogspot.com

linkedin:
http://www.linkedin.com/profile/view?id=28611299&trk=tab_pro

Shrivats

unread,
Mar 15, 2013, 11:35:53 PM3/15/13
to wohlgemuth, Jean Yang, scala-user
On Sat, Mar 16, 2013 at 8:59 AM, wohlgemuth <wohlg...@ucdavis.edu> wrote:
> i would guess that finalize should work as it does i java.
>
> http://www.scala-lang.org/api/2.7.7/scala/AnyRef.html
>
> g.

Hm, I think I went a bit overboard when I said "no finalize" like in
Java :) I should have checked first, sorry. In this case, it would
work only if you extend AnyRef since finalize is protected like:

class Foo extends AnyRef {
def closeThis = this.finalize
}

val foo = new Foo
foo.closeThis // No guarantee that this will be GC'd, just that it
will "eventually" be GC'd

Shrivats

Naftoli Gugenheim

unread,
Mar 19, 2013, 1:11:05 PM3/19/13
to Shrivats, wohlgemuth, Jean Yang, scala-user
In Java, Scala, and any JVM language, you can't call finalize to hint that an object should be GC'd! finalize is a callback that the JVM will call when it is looking for finalizable objects. If it has a finalize method then instead of collecting it, it calls finalize and schedules it to be collected then next time around.
(Note that the above is not intended to be a precise specification.)


Nils Kilden-Pedersen

unread,
Mar 19, 2013, 2:31:43 PM3/19/13
to Naftoli Gugenheim, Shrivats, wohlgemuth, Jean Yang, scala-user
On Tue, Mar 19, 2013 at 12:11 PM, Naftoli Gugenheim <nafto...@gmail.com> wrote:
In Java, Scala, and any JVM language, you can't call finalize to hint that an object should be GC'd! finalize is a callback that the JVM will call when it is looking for finalizable objects. If it has a finalize method then instead of collecting it, it calls finalize and schedules it to be collected then next time around.

finalize has two problems: 1) It's never guaranteed to run, 2) it takes two GC cycles to get rid of (as Naftoli writes)

Alternatively, one can use a PhantomReference, which don't have the same problems.

 

Naftoli Gugenheim

unread,
Mar 19, 2013, 3:02:20 PM3/19/13
to Nils Kilden-Pedersen, Shrivats, wohlgemuth, Jean Yang, scala-user
But none of those comes close to being a destructor, which is a way to say "deallocate the memory pointed to by x."

wohlgemuth

unread,
Mar 19, 2013, 3:07:23 PM3/19/13
to Naftoli Gugenheim, Nils Kilden-Pedersen, Shrivats, Jean Yang, scala-user
correct. but thats a limitation of the JVM in general. Since its 'supposed' to take care of memory management for you, so that you don't have to work with deconstructors.

Alan Burlison

unread,
Mar 20, 2013, 5:28:36 AM3/20/13
to Naftoli Gugenheim, Nils Kilden-Pedersen, Shrivats, wohlgemuth, Jean Yang, scala-user
On 19/03/2013 19:02, Naftoli Gugenheim wrote:

> But none of those comes close to being a destructor, which is a way to say
> "deallocate the memory pointed to by x."

Why would you want to do that? That's the JVM's job. Plus stateful
cleanup and freeing memory are not the same thing.

--
Alan Burlison
--

Donald McLean

unread,
Mar 20, 2013, 10:12:04 AM3/20/13
to scala-user
I have objects that require both proper setup and proper shutdown.
Generally, objects of this type implement a trait where the setup and
shutdown functions are virtual.

In my experience, this is just about as good as it gets in any JVM
language. Just make sure that after you call the object's shutdown
method that you systemically eliminate all references to it so that
the object will be properly garbage collected.

Donald
Reply all
Reply to author
Forward
0 new messages