Garbage collection mechanism & memory management in Java

Source: Internet
Author: User

1.

  When you create an object, Java automatically allocates memory and frees the memory when the object reference does not exist.

Why is it?

Because Java uses techniques called Garbage collectors to monitor the operation of Java programs, the memory used by objects is freed automatically when objects are no longer in use.

The garbage collector runs automatically without explicitly requesting a garbage collector, and the garbage collector periodically checks the object's references and reclaims the memory used by unreferenced objects while the program is running.

You can call the static GC () method in the system class to run the garbage collector.

2.

The Java language does not require the JVM to have a GC, nor does it stipulate how the GC works.

The Java garbage collection mechanism serves all Java applications, not for a particular process, so no process can command the garbage collection mechanism to do what, how, and how much.

Java provides a default mechanism to terminate the object to release resources , this method is finalize ();

Chestnuts:

Object Sobj=new Object (), Object Sobj=null;object Sobj=new object (), Sobj=new object ();

The sobj of the first few lines meet the criteria of the garbage collection mechanism?

Answer: Lines 1th and 3rd

Reason: Because the 2nd row sobj is assigned null, line 1th conforms. The 4th line is also equivalent to assigning a value of sobj to null.

  Determine if a piece of memory space meets the principle of garbage collector collection criteria:

(1) A null value is given to the object and has not been called in the future.

(2) A new value is assigned to the object, that is, the memory space is reassigned.

A piece of memory that conforms to the garbage collector's collection principle does not necessarily mean that the memory will be collected by the garbage collector.

  Garbage collection is not a stand-alone platform

How does 3.Java manage memory?

  The memory management of Java is the allocation and release of objects.

In Java, programmers need to request memory space for each object with the keyword new (except for the base type), and all objects allocate space in the heap.

The release of an object is determined and executed by the GC. the fundamental principle of object deallocation is that the object is no longer referenced.

  This means that the memory allocation is done by the program and the memory release is done by the GC.

4. Memory leaks

Here is a simple memory leak of chestnuts:

Vector v=new vector (int i=0;i<100;i++) {     Object O=new object ();     V.add (o);     O=null;} At this point, all object objects are not freed because the variable v refers to these objects.

Reason: Because this object is added to the vector, it must also be removed from the vector

Can add another sentence: V=null;

Summarize the reason for the memory leak (one sentence): A reference to an object that remains but is never used again.

  Ways to resolve memory leaks:

Set the object field to null or remove the object from the collection (collection) to complete.

Garbage collection mechanism & memory management in Java

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.