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

LVL 12
jazzIIIloveAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
What you really need to do is to refrain from investigating the theory behind a hypothesis that we've said is wrong in the first place and start asking the real question, which is "I have a memory leak - how can i find its cause?".

Firstly (forgetting what we've just discussed), do you have any suspicions at all of where it might be? If not, you might look at Surviving Generations analysis:

https://netbeans.org/kb/articles/nb-profiler-uncoveringleaks_pt1.html
0
 
CEHJConnect With a Mentor Commented:
In theory, very little needs to be explicitly set to null in Java owing to garbage collection. Occasionally you might want to do so to 'nudge' GC if a lot of resources are involved. That isn't the case with an InputStream. Why were you worrying about memory leaks?
0
 
jazzIIIloveAuthor Commented:
HI;

The code consequently accumulates and fills up all the heap space. I was thinking maybe this setting null is causing this. Can it?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJConnect With a Mentor Commented:
No. Setting something to null would normally do the reverse. The cause of your problem is elsewhere

e.g. NOT calling that method could cause a memory leak
0
 
dpearsonConnect With a Mentor Commented:
You get a memory leak in Java when you keep a reference to an object that you no longer are actively working with.

E.g. If you had:

List<InputStreams> myStreams ; // Declared in your main class

// Then later
myStreams.add(is) ;   // The list of streams builds up over time and is never cleared out - this is a memory leak

Like CEHJ said, setting something to null removes the reference to it, so it certainly won't cause the leak.  It may avoid a leak.

Doug
0
 
krakatoaCommented:
// 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?
0
 
jazzIIIloveAuthor Commented:
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.
0
 
CEHJConnect With a Mentor Commented:
Please correct me if i am wrong.
He's assuming it was created ;)
0
 
CEHJCommented:
:)
0
All Courses

From novice to tech pro — start learning today.