Implementing IDisposable on java.lang.Object in C#

314 views
Skip to first unread message

Renier

unread,
Mar 16, 2011, 2:56:59 AM3/16/11
to jni4net
Hi Pavel,

The java.lang.Object class in Object.cs has an internal jvm handle of
type JniGlobalHandle, which in turn inherits from SafeHandle.

SafeHandle is an IDisposable object and thus allows for deterministic
clean up. It also provides a finalizer that will clean up its
unmanaged resources if dispose is not called, but this is obviously
not ideal since the garbage collector can take very long before
calling finalize on the object.

Can you please make java.lang.Object implement IDisposable?
(And inside the implementation for Dispose call the dispose method on
the jvm handle)

This will make it possible to call dispose on proxy objects from
within C#, thus releasing the handle resources more deterministically.
It should also make calling GC.Collect() unnecessary and allow the JVM
GC to collect more efficiently.

Thanks,
Renier

Pavel Šavara

unread,
Mar 16, 2011, 3:55:26 AM3/16/11
to jni...@googlegroups.com
Renier, All,

To control lifetime explicitly you could
Cast it to IJvmProxy and
((IJvmProxy)javaInstance).JvmHandle.Dispose()

I'm open to discuss explicit disposal problem.

I had that approach in very early version already. It seemed to me
that GC is better approach for general use cases.
IDisposable seems to me like overkill. Few reasons
- GC will behave different to object which implement IDisposable
- Dispose() method doesn't look nice in intellisense of each proxy (if
we make it public) and may clash with Java side API.
- Consider ClrProxy on JVM/Java side too. We could add
java.io.Closeable (or something) to it.

I don't believe most people will manage the handles explicitly. It is
quite difficult to realize how many proxies are created all the time.
jni4net usage feels very similar to normal use of the OOP and people
don't notice that they deal with proxies most of the time.

People who really care about performance/leaks like yourself should be
able to address it. I propose we make it easier.
- by adding static Bridge.Dispose(IJvmProxy obj)
- we may allow you to register callback for proxy instantiation, so
you could collect all proxies and dispose them as necessary.

Hey, other people, please share your thoughts too :-)

Cheers
Pavel

> --
> You received this message because you are subscribed to jni...@googlegroups.com
> http://groups.google.com/group/jni4net?hl=en-GB?hl=en-GB
> http://jni4net.sf.net/
>

Reply all
Reply to author
Forward
0 new messages