Java Resource leaks

Source: Internet
Author: User



1. Resource leaks under normal circumstances

FileOutputStream fos = newfileoutputstream (New File ("test.txt"));

...


The FOS is no longer closed.


2. Resource leaks in exceptional circumstances

1)

FileOutputStream fos = newfileoutputstream (New File ("test.txt"));

Fos.write (7); Write () has an exception, causing fos.close () to not be executed.

Fos.close ();


2)

FileOutputStream fos1 = Newfileoutputstream (New File ("Test1.txt"));

FileOutputStream Fos2 = Newfileoutputstream (New File ("Test2.txt"));


Fos1.close (); Close () has an exception, causing Fos2 to have no close. --close () calls flush (), so it is possible to throw an exception.

Fos2.close ();

The Try/catch anomaly mechanism of Java is a checked exception mechanism. The purpose is to report errors that occur while the program is executing. When an exception occurs, the program cannot continue execution and exits. Under such circumstances, it is trifles to explore the "resource leakage", which is of little significance.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.