This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

You can start with this article I wrote yesterday – garbage collection in Java

Difficulty level: Intermediate In Java, object destruction is the responsibility of the garbage collector module, and objects without any reference are eligible for garbage collection. Here are some important output questions about garbage collection.

The program

Program a

Predict the output of the following Java program:

public class Test
{
	public static void main(String[] args) throws InterruptedException
	{
		String str = new String("Haiyong");
			
		// Make STR conform to GC conditions
		str = null;
			
		// Call the garbage collector
		System.gc();
			
		// Wait for the GC to complete
		Thread.sleep(1000);
	
		System.out.println("end of main");
	}

	@Override
	protected void finalize(a)
	{
		System.out.println("Finalize method called"); }}Copy the code

Click here to skip to the answer

Program 2

public class Test
{
	public static void main(String[] args) throws InterruptedException
	{
		Test t = new Test();			
		// Make t eligible for garbage collection
		t = null;			
		// Call the garbage collector
		System.gc();			
		// Wait for the GC to complete
		Thread.sleep(1000);	
		System.out.println("end main");
	}
	@Override
	protected void finalize(a)
	{
		System.out.println("Finalize method called");
		System.out.println(10/0); }}Copy the code

Click here to skip to the answer

Application of three

public class Test
{
	static Test t ;	
	static int count =0;	
	public static void main(String[] args) throws InterruptedException
	{
		Test t1 = new Test();			
		// Make T1 eligible for garbage collection
		t1 = null; // line 12			
		// Call the garbage collector
		System.gc(); // line 15			
		// Wait for the GC to complete
		Thread.sleep(1000);	
		// Make t eligible for garbage collection
		t = null; // line 21			
		// Call the garbage collector
		System.gc(); // line 24	
		// Wait for the GC to complete
		Thread.sleep(1000);			
		System.out.println(Call finalize method+count+"Time");		
	}	
	@Override
	protected void finalize(a)
	{
		count++;		
		t = this; // line 38			}}Copy the code

Click here to skip to the answer

Application of four

public class Test
{
	public static void main(String[] args)
	{
		// How many objects are eligible for garbage collection after this row?
		m1(); // Line 5
	}

	static void m1(a)
	{
		Test t1 = new Test();
		Test t2 = newTest(); }}Copy the code

Click here to skip to the answer

Application of five

public class Test
{
	public static void main(String [] args)
	{
		Test t1 = new Test();
		Test t2 = m1(t1); // line 6
		Test t3 = new Test();
		t2 = t3; // line 8
		
	}	
	static Test m1(Test temp)
	{
		temp = new Test();
		returntemp; }}Copy the code

Click here to skip to the answer


The second half of the article is the output and parsing of the program


Output and parsing

Program one output

Output:

end of main
Copy the code

Explanation:

We know that the garbage collector calls the Finalize () method on an object before destroying it. But here, the trick is that STR is a String object, not a Test. Therefore, call the Finalize () method of the String class on STR (if overridden in the String class). If a class does not override the Finalize method, then the Object class’s Finalize () method is called by default.

Program two output

Output:

Call finalize method end mainCopy the code

Description:

When the garbage collector calls the Finalize () method on an object, it ignores all exceptions raised in that method, and the program terminates normally.

Program three output

Output:

Call the Finalize method1Copy the code

Description:

After line 12, T1 can be garbage collected. So when we call the garbage collector in line 15, the garbage collector calls the Finalize () method on T1 before destroying it. But in the Finalize method, at line 38, we refer to the same object again by t, so after line 38, the object no longer meets the garbage collection criteria. Therefore, the garbage collector does not destroy objects.

Now, again in line 21, we make the same object eligible for garbage collection again. Here, we want to be clear about the fact about the garbage collector that it will call the Finalize () method on a particular object at exactly one time. Since the Finalize () method has already been called on this object, the garbage collector will now destroy it without having to call the Finalize () method again.

4.

Question: After executing line 5, how many objects qualify for garbage collection?

Answer:

2
Copy the code

Description:

Because T1 and T2 are local objects to the M1 () method, they are eligible for garbage collection after the method completes unless either is returned.

5.

Question: How many objects qualify for garbage collection after line 8 is executed?

Answer:

1
Copy the code

Description:

By line 8, the only unreferenced object is the one generated on line 6. Remember that “Java passes strictly by value,” so the reference variable T1 is not subject to the M1 () method. We can check it using the Finalize () method. The statement “system.out.println (this.hashCode ())” in the Finalize () method prints the hashcode value of the object that called the Finalize () method, and then compares that value to the hashcode values of other objects created in the main method.


That’s all for this article

Related articles:

Java program Java exercises 】 【 | at the output of the first set of (analysis) the output of the Java program Java exercises 】 【 | second (analysis) the output of a Java program | Java exercises 】 【 a third (parsing) the output of a Java program | Java exercises 】 【 4 sets (including parsing) The output of a Java program | Java exercises 】 【 5 sets (including parsing) the output of the Java program Java exercises 】 【 | 6 sets (including parsing) the output of a Java program | Java exercises 】 【 7 sets (including parsing) the output of a Java program | Java exercises 】 【 8 sets (including parsing) The output of a Java program | Java exercises 】 【 9 sets of (including parsing)

I have been writing a tech blog for a long time and this is one of my tech articles/tutorials. Hope you like it! Here is a summary of all my original and work source code: GitHub, and this is I recently just set up their own blog: Haiyong. Site, there is no content, put some HTML games, interested in can try, source code can be their own F12 copy, or directly find me to.

If you do learn something new from this post, like it, bookmark it and share it with your friends. 🤗 Finally, don’t forget ❤ or 📑 for support