Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Resource leak

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Resource leak

    what does this mean? and how do I fix this issue?

    resource leak:'input1' is never closed

    many thanks.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Resource leak

    If a class implements the interface Closeable and you create a new instance of this class then you are also supposed to call the "close()" method on the object at some point in time.
    If you dont the compiler might generate a warning just like the one you have posted.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Resource leak

    Thank you Cornix.
    I actually just ended up converted local variable to field. It fixed the problem, but I have no idea why this fixes the problem. Do you mind explaining please. thank you

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Resource leak

    Depending on if you are using basic console I/O with the Scanner class,
    you can ignore the warning. Like Cornix suggested, it regards mainly file
    streams, that a file object must be closed when opened. When you use
    class Scanner, you create an instance of the object, and this opens
    the Scanner port - so the compiler is reminding you to close it.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    wamidh (September 11th, 2014)

  6. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Resource leak

    Quote Originally Posted by wamidh View Post
    Thank you Cornix.
    I actually just ended up converted local variable to field. It fixed the problem, but I have no idea why this fixes the problem. Do you mind explaining please. thank you
    Because the compiler is not smart enough to detect that you are doing something wrong and therefor does not give out a warning. But the actual problem is not solved at all, it has just been hidden from the view of the compiler.

    The actual solution would be to call the close() method on your object once you dont need it anymore, as I have explained in my first post.

  7. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Resource leak

    oh. how do I use close()
    do I type in close(input1) ;
    thank you Cornix

    --- Update ---

    ah, thank you Ada

  8. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Resource leak

    If you have a variable of type Closeable then it has a close() method.
    For example:
    Closeable someCloseable = new Scanner(System.in);
    someCloseable.close();

    Because the Scanner class is implementing the interface Closeable. Other Closeables could be all kinds of Output- or InputStream's

  9. The Following User Says Thank You to Cornix For This Useful Post:

    wamidh (September 11th, 2014)

  10. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Resource leak

    If you are using Java 7 or above, you can use the try-with-resource:
    try (Closeable someCloseable = new Scanner(System.in)) {
    ...
    }

    If I am to understand the Oracle documentation correctly, that should automatically close the resource when the try block is exited.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. The Following User Says Thank You to aussiemcgr For This Useful Post:

    wamidh (September 11th, 2014)

  12. #9
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Resource leak

    Quote Originally Posted by aussiemcgr View Post
    If you are using Java 7 or above, you can use the try-with-resource:
    try (Closeable someCloseable = new Scanner(System.in)) {
    ...
    }

    If I am to understand the Oracle documentation correctly, that should automatically close the resource when the try block is exited.
    Yes, and this is the best habit to get into. No more try { ... } finally { ... } blocks for something as mundane (and easy to forget) as invoking .close(). Especially when your finally block has to handle even more checked exceptions.

  13. The Following User Says Thank You to jdv For This Useful Post:

    wamidh (September 11th, 2014)

  14. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Resource leak

    Only if the class is implementing the interface AutoCloseable which the Scanner class actually does, so it should work.
    But not every Closeable is neccessarily an AutoCloseable as well.

  15. The Following User Says Thank You to Cornix For This Useful Post:

    wamidh (September 11th, 2014)

  16. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Resource leak

    thank you everybody!

Similar Threads

  1. issue with scanner class-resource leak
    By davep1983 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 9th, 2014, 02:00 PM
  2. Eclipse Error: Scanner resource leak
    By NorrinGalan in forum Java IDEs
    Replies: 3
    Last Post: February 7th, 2014, 04:27 AM
  3. Java application Memory Leak
    By Prem2310 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 9th, 2013, 08:42 AM
  4. SImpeJDBCinsert leads to memory leak
    By justyStepi in forum JDBC & Databases
    Replies: 3
    Last Post: May 22nd, 2013, 11:26 AM
  5. Resource leak: 'in' is never closed..?
    By missm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2012, 02:01 PM