Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

setting a inputstream to null after closing it. Bad practice? Memory leaks?

Posted on 2014-10-16
9
Medium Priority
?
212 Views
Last Modified: 2014-10-20
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

0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1600 total points
ID: 40384054
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 40384073
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1600 total points
ID: 40384084
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 400 total points
ID: 40385418
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 40385496
// 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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 40387619
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1600 total points
ID: 40387634
Please correct me if i am wrong.
He's assuming it was created ;)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1600 total points
ID: 40387656
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 40391838
:)
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question