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.