Reputation: 51
I have a class Response
public class Response
{
public Response(String response,String platform,String url)
{
this.response =response;
this.platform = platform;
this.url = url;
}
...
}
saving this object in table, from DAO
{
Response response = null;
response = new Response(jsonObject.toString(), platform, url);
sendgridResponseDAO.save(response);
...
}
or
{
sendgridResponseDAO.save(new Response(jsonObject.toString(), platform, url));
}
Functionally both are the same. Please help me to understand in the second way, how and when the garbage collector frees those new objects? Will the second way create memory issues and slow down the system? Which one is efficient?
Upvotes: 0
Views: 79
Reputation: 308061
What you call a "reference name" is just another reference that points to a given object, in this case it's a local variable.
As soon as the object is unreachable from any garbage collection root, it will be garbage collected.
As long as the save
method is running the Response
object will be reachable via the argument of that method in both cases (whether or not you assigned it to a local variable first) and chances are that the save
method will store a reference to that object somewhere (so that a later commit can actually save the data), so in this specific case the local variable is unlikely to cause any difference in the garbage collection behaviour.
Upvotes: 0