Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Freeing Memory

After your program is done with a certain object, is it good practice to set that object to null in order to free memory?  Or what is preferred method if there is one?
0
sre22
Asked:
sre22
  • 2
  • 2
1 Solution
 
lwinkenbCommented:
The java garbage collector will automatically free the memory when there are no more references to it.
0
 
sre22Author Commented:
>>when there are no more references to it.

what do you mean by this?  Does my program know when will be the last time I use an object and automatically delete it after the last time?
0
 
lwinkenbCommented:
>>what do you mean by this?  Does my program know when will be the last time I use an object and automatically delete it after the last time?

Pretty much, yes.  The JVM keeps track of how many references an object has (among other things), and when it reaches 0, that object's memory is freed up for use.  The actual method the JVM uses is a bit more complex than a simple reference counter, but I hope you get the picture.
0
 
sre22Author Commented:
sounds good to me.  Thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now