• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What is meant by resource leak?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anyone please explain what is meant by resource leak in general?
what are the causes for it?
How to resolve it?

Thank you.
 
Saloon Keeper
Posts: 24295
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have a long-running application, it is very important that you properly put away your toys when you are done playing with them.

There are a finite number of TCP/IP ports on the OS. Likewise, there are a finite number of file handles. And of course, a finite amount of RAM.

If you don't close your connections and files and release unused objects, they hang around. And if you continually get new ones, eventually you'll run out of things to get. At that point the webapp and/or server crashes. And sometimes you're lucky to get that far, because in some cases, you're leaking OS resources, meaning that you can hurt other apps or even the OS itself.

Simply de-referencing such objects is often not enough. If you remove all references and wait for garbage collection, there's no way to predict when things will get reclaimed. And in the case of some OS service objects, I'm not entirely certain that the underlying OS service objects will get released even when the associated Java object gets garbage-collected. And, of course, there's always the possibility that somewhere a reference got forgotten and wasn't released.

I once worked with someone who didn't realize it was important to close files after use. He didn't think anything about the fact that the webapp server had to be re-booted every few days. To him, software is inherently unreliable. In actual fact, it was his programming that was unreliable.
 
vishnu priyag
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

Consider, the java code,
1. Open a file, and reads the content from the file using IO Streams
2. Send the content to network.

Please correct me if i am wrong,
here while reading the content from file, File handles being used and
for sending data through network, TCP/IP is being used.

If i don't close the TCP/IP connection, all the TCP/IP connections will be used.
What happens when i don't close the file? does all the file handles get consumed?
What about the streams?, does streams also use TCP/IP?

Thank you.
 
Marshal
Posts: 74004
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vishnu priyag wrote:. . . What happens when i don't close the file? does all the file handles get consumed? . . .

Yes, eventually.
 
Tim Holloway
Saloon Keeper
Posts: 24295
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, there are a finite number of both file handles and network connection resources in the system. You can frequently change that number, and in fact for products like the Oracle database servers, but the number is still finite, and so it's just changing when you run out, not "if".

Many OS resources are automatically reclaimed when an application terminates, but that's not likely to be much comfort when the reason the application terminated was that the JVM crashed.
 
vishnu priyag
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, what is the relation between java io streams and file handles when I use io streams to make connection to files or resources like socket ?
 
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nobody said the Magic Words yet, so I will be uncharacteristically brief and say when it comes to this topic:

AutoCloseable, Closeable and "Try with Resources" are your best friends!!


 
Campbell Ritchie
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's expand on what Jesse said: for more information, look in the Java™ Tutorials.
 
vishnu priyag
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the resources, but my question is, does java io streams uses TCP/IP to interact with resources like files that are present in the system where the application is running?
 
Jesse Silverman
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IOStreams normally uses the regular plain old file system to read and write local files, no TCP/IP involved whatsoever.

If you are expecting to read stuff *not* on your local machine, you still wouldn't be messing about with sockets and such normally -- they might be involved under the covers.

Using the awesome new functionality of "nio.2" which confusingly, is still in the java/nio/file package, you have access to stuff like this:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Path.html#of(java.net.URI)

which references this:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/URI.html

Which is to say, a lot of (relatively) recent work has gone into Java towards making accessing files NOT on your local machine relatively easy and simple, and looking, once you have the resource established, just like a local file while you are working with it.

So classic IOStreams, not so convenient or easy to work with non-local files, modern nio.2 Path/Paths/Files stuff, more integrated/similar to working with local files.

The aforementioned "Try With Resources" idiom looks the same regardless of which AutoCloseable resource type you are working with, one of several benefits.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic