![]() Handling exceptions from all possible execution paths is laborious and difficult to get right. ![]() This code is erroneous: If copying throws an exception, and if the output.close() statement in the finally block throws an exception, then the input stream will be leaked. In early versions of Java, developers typically used the try-finally construct to ensure that resources were released even if an exception occurred while copying: FileInputStream input = null Consider the following code that copies data from one file to another. Resource leaks can be surprisingly common. The resource, still considered by the operating system to be in use, has leaked. If the program fails to do this before the GC reclaims the object then the information needed to release the resource is lost. For such objects, it is insufficient merely to reclaim the object's memory the program must also release the underlying resource back to the operating system, typically by calling the object's close method. However, some objects represent a resource provided by the operating system, such as an open file descriptor or a block of native memory. Java programs enjoy automatic memory management, wherein the JVM's garbage collector (GC) reclaims the memory used by an object when the object is no longer needed.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |