Garbage collection:
We have seen a lot about how we create new objects. The converse question is: how do we get rid of them? If objects simply continued to exist once created, it is quite possible that we would run out of memory. When an object is created, the Java interpreter will create enough space for it to exist, including enough memory for its instance variables. When an object is no longer in use, the system will destroy it and return the space allocated to the object to a reservoir of free memory.
For example, if you had a method as follows:
public void greetUser (String username)
{
String greeting = "How do you do ";
System.out.println(greeting + username + "?");
}
then the system would create space for the String object referenced by greeting to hold the characters in the string literal. When the method completes its processing, the space allocated to the object referenced by greeting will be marked as unused. This is safe to do because the reference is declared in the method, and it no longer exists outside the method. (It is a
local variable.) Periodically, the Java run-time system returns to the reservoir of free space all the memory that is occupied by unreferenced objects. This process is known as garbage collection. Garbage collection can cause a program's execution to slow down, so you would probably not want it in a real-time system (for example, in an aeroplane fly-by-wire system). On the other hand, it is a much safer approach than relying on a programmer to decide when memory is free to be returned to the system, so in a general-purpose language such as Java it is seen as a good feature. Some other languages, including C, leave garbage collection up to the programmer. This is often a source of subtle errors.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Garbage collection homework and assignments? Live Garbage collection experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Garbage collection homework help, java assignment help and Garbage collection projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours