Java – want to know that create = null instead of out Consequences of close() Out is an instance of fileoutputstream

I want to know the consequences of making the output stream null instead of closing it If I do out = null, will this lead to resource leakage?

Solution

It may be, although it depends on what the stream is (eventually) connected to

Another question is whether it is important It also depends on

>If the stream contains a buffer in the output pipeline, allocating null instead of closing may result in the loss of buffer data. > If you do this repeatedly, the leaked resources may accumulate and eventually cause your application to fail because it has run out of file descriptors. > On the other hand, if you leak resources slowly, you may garbage collect and finalize the stream before there is a shortage of file descriptors (the finalize () method calls close()...)

Either way, it's best to call close()... And ensure that it is closed by using "try / finally" or "try with resources" Allocating null instead of calling close () is a hassle

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>