Will this method cause a memory leak when it throws an exception?

user2997204

Will this method cause a memory leak when it throws an exception?

public static void warnUser(String name) {
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(dir + "warnings.txt", true));
        writer.newLine();
        writer.write(name);
        writer.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.err.println("error giving warning to: " + name);
    }
}

Is this better?

    public static void warnUser(String name) {
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir + "warnings.txt", true))) {
        writer.newLine();
        writer.write(name);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.err.println("error giving warning to: " + name);
    }
}
meriton

A memory leak? No, once execution leaves this method, be it through return or throwing an exception, the BufferedWriter object will no longer be reachable, and becomes eligible for garbage collection.

However, as you are not invoking the close method when an exception is thrown while writing to the file, the file will remain open, preventing anybody from using it, and possibly exhausting the limited number of files that the operating system can keep open at any given time, until finally the garbage collector gets around to collecting the object, which will trigger its finalizer which closes the file, but you don't know when that is (it can easily take hours if you're unlucky). This is why operating system resources such as files should be closed right when your program no longer needs them. That's why InputStreams have a close method, and Java has a try-with-resources statement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Will this method cause a memory leak when it throws an exception?

From Dev

Why callback method for asynchronous network call can't cause memory leak when activity finished?

From Dev

Ways to avoid memory leak when exception thrown

From Dev

why does not implementing equals method in Java cause memory leak

From Dev

Does this cause memory leak when I call the deconstructor?

From Dev

Does this cause memory leak when I call the deconstructor?

From Dev

Memory leak when use newInsance() method

From Dev

Will this cause a memory leak in c++?

From Java

Is it possible to cause a memory leak in Rust?

From Dev

Finding the cause of a memory leak in Ruby

From Dev

will this cause a memory leak in C#

From Dev

Will below code cause memory leak?

From Dev

Will this cause a memory leak in c++?

From Dev

Will emptying an Array = [ ] cause memory leak?

From Dev

Does this Bitmap cause a memory leak?

From Dev

Verifying behavior when testing a method that throws an exception

From Dev

Where is memory leak in this method?

From Dev

Memory Leak in Ctypes method

From Dev

In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

From Dev

OracleBulkCopy Memory Leak(OutOfMemory Exception)

From Java

Does never resolved promise cause memory leak?

From Dev

Will returning nil in init cause a memory leak?

From Dev

Would this simple code cause a memory leak?

From Dev

Is subMap of NavigableMap going to cause memory leak?

From Dev

What is the cause of this strange Scala memory leak?

From Dev

Can the value of a ConditionalWeakTable cause a memory leak?

From Dev

recursive calls to ajax cause memory leak?

From Dev

Memory leak - probably 'cause of to many eventhandlers

From Dev

Is subMap of NavigableMap going to cause memory leak?

Related Related

  1. 1

    Will this method cause a memory leak when it throws an exception?

  2. 2

    Why callback method for asynchronous network call can't cause memory leak when activity finished?

  3. 3

    Ways to avoid memory leak when exception thrown

  4. 4

    why does not implementing equals method in Java cause memory leak

  5. 5

    Does this cause memory leak when I call the deconstructor?

  6. 6

    Does this cause memory leak when I call the deconstructor?

  7. 7

    Memory leak when use newInsance() method

  8. 8

    Will this cause a memory leak in c++?

  9. 9

    Is it possible to cause a memory leak in Rust?

  10. 10

    Finding the cause of a memory leak in Ruby

  11. 11

    will this cause a memory leak in C#

  12. 12

    Will below code cause memory leak?

  13. 13

    Will this cause a memory leak in c++?

  14. 14

    Will emptying an Array = [ ] cause memory leak?

  15. 15

    Does this Bitmap cause a memory leak?

  16. 16

    Verifying behavior when testing a method that throws an exception

  17. 17

    Where is memory leak in this method?

  18. 18

    Memory Leak in Ctypes method

  19. 19

    In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

  20. 20

    OracleBulkCopy Memory Leak(OutOfMemory Exception)

  21. 21

    Does never resolved promise cause memory leak?

  22. 22

    Will returning nil in init cause a memory leak?

  23. 23

    Would this simple code cause a memory leak?

  24. 24

    Is subMap of NavigableMap going to cause memory leak?

  25. 25

    What is the cause of this strange Scala memory leak?

  26. 26

    Can the value of a ConditionalWeakTable cause a memory leak?

  27. 27

    recursive calls to ajax cause memory leak?

  28. 28

    Memory leak - probably 'cause of to many eventhandlers

  29. 29

    Is subMap of NavigableMap going to cause memory leak?

HotTag

Archive