Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

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?

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!");
	}

Open in new window

SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jazzIIIlove

ASKER

HI;

The code consequently accumulates and fills up all the heap space. I was thinking maybe this setting null is causing this. Can it?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
// 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?
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)