Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDisposable metaphor in Java?

Tags:

java

.net

As a java developer getting into .NET I'd like to understand the IDisposable interface. Could somebody please try to explain this and how it differs from what happens in Java? Thanks.

like image 987
gil Avatar asked Nov 20 '09 03:11

gil


People also ask

What is the use of IDisposable?

We should use an IDisposable design pattern (or Dispose Pattern) when we need to dispose of unmanaged objects. For implementing the IDisposable design pattern, the class which deals with unmanaged objects directly or indirectly should implement the IDisposable interface.

What is an IDisposable object?

This article about the IDisposable pattern is the continuation of my previous article “Object LifeTime in . NET Framework". IDisposable is an interface that contains a single method, Dispose(), for releasing unmanaged resources, like files, streams, database connections and so on.

Is IDisposable called automatically?

By default, the garbage collector automatically calls an object's finalizer before reclaiming its memory. However, if the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer.

What is IDisposable interface & finalize dispose pattern in GC?

Idisposable interface and Finalize Dispose patternGarbage collector only cleans managed code and if any unmanaged code it finds it doesn't reclaim its memory so to remove unused objects we need something. In this case, at least we have a responsibility to call these kinds of which free unmanaged code objects.


2 Answers

I wrote a detailed series of articles on IDisposable.

The basic idea here is that sometimes you DO need deterministic disposal of resources. IDisposable provides that mechanism.

For example, say you have a control in a Window. When this is created, it creates a windows handle (HWND) internally. When you remove the control from the Window, and its no longer used, the control becomes eligible for garbage collection - but it does not get collected right away. In fact, there are no guarantees as to how long it will be before it is collected.

Until the GC runs and processes the orphaned control, it will still use resources, since it's still holding the HWND.

IDisposable provides a means for objects that contain code that needs cleanup separate from the GC to be explicitly cleaned up by the user of the object. In the control's case, we can call myControl.Dispose(), which will immediately, synchronously cleanup the "native" resources (HWND) used by the control.

like image 178
Reed Copsey Avatar answered Sep 16 '22 13:09

Reed Copsey


With java 1.7 there is the new introduced try-with-resource statement.

try (BufferedReader br = new BufferedReader(new FileReader(path))) {
    return br.readLine();
}

The objects used here must implement AutoCloseable interface. It is not exactly the same as IDisposable, but the close() is called automatically in the finally. This gives the oportunity to implement similar behavior.

The code above is the same as

BufferedReader br = new BufferedReader(new FileReader(path));
try {
    return br.readLine();
} finally {
    if (br != null) br.close();
}

Read more about this in java tutorial. The samplecode is coming from there.

like image 29
Christian13467 Avatar answered Sep 16 '22 13:09

Christian13467