jazzIIIlove
asked on
setting a inputstream to null after closing it. Bad practice? Memory leaks?
Hi;
I have the following code and not sure whether it will create a memory leak or not? What are the risks setting a inputstream to null after closing it. Bad practice?
I have the following code and not sure whether it will create a memory leak or not? What are the risks setting a inputstream to null after closing it. Bad practice?
public void close() throws IOException {
System.out.println("Calling Test.close()...");
if (is != null) {
is.close();
is = null;
}
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getId() + ": Test.close() called!");
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
// Then later
myStreams.add(is) ; // The list of streams builds up over time and is never cleared out - this is a memory leak
Just curious Doug - how would this differ from a regular assignment?
ASKER
Hi Doug,
I guess your example would give a null pointer exception, not a leak as you didn't actually create the object. Please correct me if i am wrong.
I guess your example would give a null pointer exception, not a leak as you didn't actually create the object. Please correct me if i am wrong.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
ASKER
The code consequently accumulates and fills up all the heap space. I was thinking maybe this setting null is causing this. Can it?