Java garbage Collection

Source: Internet
Author: User
Tags throwable

In Java, when there is no reference to the object, it becomes garbage, and if it is not reclaimed in time, freeing up the memory, the garbage will accumulate more, and eventually out of MEMORY!,JVM will end up running.

Some people wonder: we usually code when not show the destruction of objects, how the program run well?

This is to talk about today's protagonist, the JVM's Guardian thread GC,GC is a garbage collector, according to a certain algorithm, the release of garbage objects, at the same time to defragment the memory, to ensure high availability of memory, maintain the normal operation of the process.

Let's look at the following section of 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 created an object called "New Demodatatype ()", and because there is no reference, it quickly becomes a garbage object, but is it bound to be recycled? At least the above results indicate that there is no recycling, because the JVM is full of memory and is belabour to reclaim you, I am a resource-intensive ^_^.

There is a static method under the Java.lang.System class GC (), which invokes it actively, can notify the GC: Hey, I have garbage here, come and help me. However, the GC does not necessarily respond to your request at once, and it may be a traffic jam and it is unclear when it arrives. Perform GC () look at the results:

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!

The work before garbage collection!

Or: Work before the garbage collection!

over!

The GC worked because it was idle, so it responded in time, but it was not immediately executed, accidental.

The GC performs a finalize () method before garbage collection, and we know that the GC only knows those objects that are applied through new, and if some of the memory is created by an abnormal means, then the GC will circle.

The role of Finalize () is to do some garbage collection before the release of resources, such as some GC can not be recycled resources, such as "non-new" memory, not closed IO and so on. But be sure not to hope in Finalize () to release resources, GC may not be reachable, will cause memory leaks.

Under what circumstances will the GC be triggered?

1) GC is the daemon thread, and as a member of the service industry, it is the lowest priority, so it starts when the app is idle.

2) When memory is low, the application is on the verge of death, the GC will come forward, eliminate garbage, release memory, and if the memory leaks are serious, the JVM will stop running.

GC work also consumes system resources, how to avoid unnecessary overhead?

1) do not produce garbage: reduce the use of temporary objects; avoid the use of empty

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

3) Clear the life cycle of the object, active destruction

4) Avoid centralizing the production of a large number of useless objects, such as large string concatenation

5) Coding the process of memory leakage, to ensure the rigor of the code.

Attention to old ginger talk about technology, number: Helojava, or scan the following QR code.


One post per day, technical chicken soup.

Java garbage Collection

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.