Java Virtual machines (2)

Source: Internet
Author: User

Garbage collection mechanism

Example code:

GC Test class, the base date is for the output to take time, flush the Finalize method, when performing garbage collection, call

public class Gctest extends date{

@Override
Public String toString () {
Return "Time" + this.gettime ();
}

/**
* This method is executed when the GC is executed.
* @throws Throwable
*/
@Override
protected void Finalize () throws Throwable {
Super.finalize ();
System.out.println ("object is recycled" + this.gettime ());
}
}

There's only one way to consume a lot of memory in this class.
public class OutOfMemory {

/**
* Consumes a lot of memory
*/
public static void Takememory () {
string[] array = new string[50000];
for (int i = 0; i<array.length; i++) {
for (int j = ' a '; j<= ' z '; j + +) {
Array[i] + = (char) j;
}
}
}

}

public class Main {

public static void Main (string[] args) {

Strong references
Gctest gctest = new Gctest ();
A soft reference may be useful but can be consumed by a GC when it takes up a lot of memory.
SoftReference reference = new SoftReference (new Gctest ());
Weak references must be recycled as long as the GC is called
WeakReference weakreference = new WeakReference (new Gctest ());
Virtual reference
Referencequeue queue = new Referencequeue ();
Phantomreference phantomreference = new Phantomreference (new Gctest (), queue);
1 gctest = null;
2        System.GC ();

Consumes a lot of memory
3 outofmemory.takememory ();
}

}

There are several scenarios after the operation
1. Also comment out 123 Gctest will not be recycled
2. Comment out 1 gctest will not be recycled (and will not be recycled at the end of the System.GC)
3. Comment out that the gctest will not be recycled (memory is sufficient)
4. Comment out 12, gctest will not be recycled
5. Comment out 2 or 3 gctest will be recycled

Garbage collection algorithm
1. Mark clear: Divided into two stages, marking phase and elimination phase disadvantage: This will cause memory fragmentation, discontinuous, if there is a list length of 4 but now only 3 continuous slices, need to re-open memory space, there is a problem.

2. Replication algorithm: Divide memory by memory capacity by two blocks of equal size. Use only one piece at a time, and when the piece of memory is full, copy the surviving object to another piece and clear out the memory that was used.

3. Marker Grooming algorithm: The tagging stage is the same as the tag clear algorithm, but instead of cleaning up the object, it moves the surviving object to one end of the memory. The object outside the end boundary is then cleared.

Garbage collector:

Garbage collection algorithm is the theoretical basis of garbage collector, and garbage collector is its concrete implementation. Here are some of the garbage collectors available from the Hotspot virtual machine.

Serial/serial old

The oldest collector, which is a single-threaded collector, must suspend all user threads when it is used for garbage collection. Serial is for the new generation of collectors, using the copying algorithm, and serial old is for the Laosheng generation of collectors, using mark-compact algorithm. The advantage is simple and efficient, and the disadvantage is that the user thread needs to be paused.

Parnew

A multithreaded version of seral/serial old that uses multiple threads for garbage collection.

Parallel Scavenge

A new generation of parallel collectors that do not need to pause other threads during recovery, using the copying algorithm. The collector differs from the first two collectors in order to achieve a manageable throughput.

Parallel old

Parallel Scavenge's Laosheng version, using Mark-compact algorithm and multithreading.

Cms

The current Mark sweep collector is a concurrent collector with the goal of minimizing the payback time, thus using the mark-sweep algorithm.

G1

The leading edge of G1 (garbage first) collector technology is a service-oriented collector that maximizes CPU and multicore environments. is a parallel and concurrent collector that can establish a predictable pause-time model.

Java Virtual machines (2)

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.