Java garbage collection and java garbage collection

Source: Internet
Author: User

Java garbage collection and java garbage collection

In java, when the object does not have any reference, it becomes garbage. If it is not recycled in time, the memory will be released, and the garbage will accumulate more and eventually out of memory !, Jvm stops running.

Some people wonder: we usually do not display the destruction of objects during encoding. How can the program run well?

This will talk about the main character of today. The jvm daemon thread GC is a garbage collector that releases junk objects from time to time according to certain algorithms, at the same time, the memory fragments are organized to ensure the high availability of the memory and maintain the normal operation of the process.

Let's look at the following code:

Public class DemoDatatype {

Public static void main (String [] args ){

New DemoDatatype ();

System. gc ();

System. out. println ("over! ");

}

@ Override

Protected void finalize () throws Throwable {

System. out. println ("work before garbage collection! ");

Super. finalize ();

}

}

Program output: over!

We have created an object "new DemoDatatype ()". Since it is not referenced, it will soon become a junk object, but will it be recycled? At least the above results indicate that there is no recycle, because the jvm memory is sufficient, and it is very resource-consuming for me to collect you.

There is a static Method gc () in the java. lang. System class. You can call it to notify the GC: Hi, I have garbage. Come and help me collect it. However, GC does not necessarily respond immediately to your request. It may be a traffic jam. It is unclear when it will arrive. Run gc () to check the result:

Public class DemoDatatype {

Public static void main (String [] args ){

New DemoDatatype ();

System. gc ();

System. out. println ("over! ");

}

@ Override

Protected void finalize () throws Throwable {

System. out. println ("work before garbage collection! ");

Super. finalize ();

}

}

Program output: over!

Work before garbage collection!

Or: Work before garbage collection!

Over!

GC is working, because it is idle, so it responds in time, but it is not executed immediately, by accident.

Before garbage collection, GC executes the finalize () method. We know that GC only knows the objects applied through new. If some memory is opened through abnormal means, GC will be covered.

Finalize () is used to release resources before garbage collection, such as resources that cannot be recycled by gc, such as non-new memory and I/O that are not closed. But do not expect finalize () to release resources. GC may not be reachable and may cause memory leakage.

Under what circumstances will GC be triggered?

1) GC is a daemon thread. As a member of the Service Industry, GC has the lowest priority. Therefore, GC is started only when the application is idle.

2) When the memory is insufficient, the application is on the verge of death. GC will come forward to eliminate the garbage and release the memory. If the memory leakage is serious, the jvm will stop running.

GC also consumes system resources. How can we avoid unnecessary overhead?

1) do not make garbage: reduce the use of temporary objects; Avoid unnecessary, empty after use

2) do not actively request GC: system. gc () do not display the call

3) Identify the objects in the lifecycle and actively destroy them.

4) avoid making a large number of useless objects in a centralized manner, such as String concatenation.

5) Pay attention to memory leakage during coding to ensure code rigor.

 

Follow ginger to talk about technology, no.: helojava, or scan the following QR code.


One post per day, chicken soup for technical purposes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.