Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Resource Leaks in Java


Published on

Published in: Technology, Education
  • Be the first to comment

Resource Leaks in Java

  1. 1. Resource Leaksin Java
  2. 2. What Is a Resource Leak?And Why You Should Care About Them
  3. 3. What Is a Resource Leak?A resource leak is an erroneous pattern ofresource consumption where a program doesnot release resources it has acquired. Typicalresource leaks include memoryleaks and handle leaks.
  4. 4. Why You Should CareConsequences to users:• Poor performance• Poor stability or reliability• Denial of serviceConsequences to developers:• Time consuming to find, even with a debugger• Can reduce reliability of the testing suite by interruptingtest runs
  5. 5. Question:Java is a modern language with a garbagecollector to take care of resourcemanagement. Doesn’t that mean that I don’tneed to worry about resource leaks?
  6. 6. Answer:No!The garbage collector (GC) can help ensureunused objects on the heap are reclaimedwhen they are no longer used. But:• It cant guarantee that you will have the resources youneed, when you need them• Reference subtleties can “trick” the GC into leavingunexpected objects on the heap• Many objects, such as database connections and filehandles, are managed by the operating system - not Java
  7. 7. Consider This Code Snippet*Note:• It tries to handle exceptions while shutting down the database• It creates Connection and Statement objects on lines 43 and 44• This method is associated with a task and may run repeatedly* From an open source project
  8. 8. Best Case:1. L43: “con” allocated,connected to db.2. L44: statement created,“SHUTDOWN” executed3. Assuming no exceptions occur, the method exits4. Eventually, the GC detects that the handles are no longerused and schedule calls to their finalizers5. In a later pass, the finalizers will be called, releasing theunderlying handles6. All is good
  9. 9. Reality:1. L43: “con” allocated,connected to db.2. L44: statement created,“SHUTDOWN” executed3. Assuming no exceptions occur, the method exits4. The application continues to work with the database inother methods5. Eventually, other methods are unable to get newstatement handles and fail6. The GC never gets to detect that the handles are no longerused, or scheduled calls to their finalizers never get run
  10. 10. To Avoid the Problem:Explicitly close the handlesReplace lines 43 and 44 with something like the followingcode:Connection con = null;Statement stmt = null;try {con = DriverManager.getConnection(<url>);stmt = con.createStatement();stmt.execute("SHUTDOWN");// Perhaps some catch blocks for SQLException, etc.} finally {// if you’re paranoid, put these in a separate try/catch for SQLExceptionif (stmt != null) { stmt.close(); }if (con != null) { con.close(); }}Note that Java 7 has a simplified “try-with-resources”statement.
  11. 11. In Short…1. Remain vigilant about resource usage• Explicitly close resources when you are done with them• Be careful to handle exceptional cases, too!2. Use tools to identify cases that you miss• Static analysis (often available in IDE, too!)• Dynamic analysis3. Enjoy the extra time you’ve freed up 
  12. 12. Copyright 2013 Coverity, Inc.