0

I am new to Java so go easy on me if this is a silly question.

I just got started with Java a few days ago and the first thing I did was build a helper class for MySQL. I am using DriverManager to connect to MySQL and it does so without issue. I also have a helper class to close the ResultSet, Statement and Connection. Here is a snippet of it.

this.result.close();
this.statement.close();
this.connection.close();

this.result     = null;
this.statement  = null;
this.connection = null;

I close each object individually as well as setting them to null once finished. I read that this may help prevent memory leaks.

My concern is that in testing, I use Runtime to test memory use throughout my application and closing the above objects and setting them to null seem to have no bearing whatsoever on the memory usage displayed by Runtime. Here is my code for determining memory usage.

System.out.println("Memory Used: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));

My question is: Why, when I display the memory used directly before and directly after running my close connection code, does the displayed memory usage not change by even a single byte? Is this an issue with the way my objects are being closed, or is the actual memory used not being reflected correctly the way I think it is by Runtime

Edit: Not sure why this was marked as a duplicate, the question referenced is very different from my actual question.

Kirk Logan
  • 733
  • 2
  • 8
  • 23
  • I'm not sure why this was marked as a duplicate. The question you linked to does not even touch on `Runtime` and monitoring memory which is really the core of this question. Setting the objects to `null` is one very small part of my question. Please read the question before marking as a duplicate. – Kirk Logan Oct 22 '16 at 14:57
  • This is asking why setting objects to `null` does not seem to have any effect on the memory usage. Indeed, this is what the linked question is asking, in slighly broader terms and covering the topic fully. The linked question focuses specifically on what the effects are of setting a variable to `null` and you can also refer to http://stackoverflow.com/questions/449409/does-assigning-objects-to-null-in-java-impact-garbage-collection – Tunaki Oct 22 '16 at 15:00
  • My question is not at all answered by the post you linked. I want to know why closing/nulling the objects does not explicitly change the output displayed by `Runtime`. My question only indicates that I set the objects to null in the interest of full disclosure. I read through the post you linked to entirely and my question is very much unanswered. Please retract marking this as a duplicate. – Kirk Logan Oct 22 '16 at 15:04
  • So, why do you expect a change in the memory consumption? Because you expect the objects to be garbage collected when setting them to `null`; and that assumption is not true. [It does not help garbage collection](http://stackoverflow.com/questions/449409/does-assigning-objects-to-null-in-java-impact-garbage-collection). – Tunaki Oct 22 '16 at 15:06
  • Then when should it actually be garbage collected? By the end of the program i'm using twice the memory I expect to be because the memory from those objects appear to NEVER be freed. – Kirk Logan Oct 22 '16 at 15:11
  • If it is not freed, then there is something wrong elsewhere, maybe with the scope of those objects. Use the try-with-resources statement to handle those [like shown here](http://stackoverflow.com/questions/8066501/how-should-i-use-try-with-resources-with-jdbc). You cannot really predict when garbage collection will run, but it will definitely run and it is very very good at it. – Tunaki Oct 22 '16 at 15:15
  • Great, I will test that. Now how about you unmark as duplicate so I can actually get some feedback from the community on this as well. Not just from you. – Kirk Logan Oct 22 '16 at 15:18

0 Answers0