uuidcode
uuidcode

Reputation: 3970

Is it OK not to call close method?

Below code is the serialize method of org.springframework.util.SerializationUtils(spring-core.3.2.1.jar). As you can see, there is no oos.close method. Is it OK not to call close method?

org.springframework.util.SerializationUtils

Upvotes: 1

Views: 196

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1502806

In this particular case, it's okay - the two streams involved (the ObjectOutputStream and ByteArrayOutputStream) don't use any unmanaged resources (file handles, network handles) etc so it's okay for the garbage collector to just take care of it.

Personally I usually close even streams like that, just to be in the right habits and in case someone changes the type of the stream to one that does have unmanaged resources, but I don't think it really counts as a bug in this case.

Upvotes: 1

George Hilliard
George Hilliard

Reputation: 15952

close should be called by most sane objects when they are destroyed (garbage collected). But since you don't know exactly when that will happen, you should make it a point to call close yourself, so that the cleanup (file buffers flushed, network kissoff performed) happens when you expect it to.

Upvotes: 0

Related Questions