How to judge the object's "death" and "live" in the article, how to judge the existence of the object, in which the root search algorithm, and the GC roots have access to determine the object's life and Death attribute. So, what is the reason that the Java virtual machine sends the memory leak, has the following two main features:
(1) The Assigned object is accessible, that is, in the roots graph, there is a pathway with GC;
(2) But the object is no longer used in future programs, that is, the object is useless.
With the above two features, it is possible to determine that memory leaks are not collected by GC, but still occupy memory.
A simple example is as follows:
Package cn.com.yy;
Import Java.util.Vector;
/**
* Simple Memory leak example
* @author yy
* @time 2014-10-3 a.m. 10:41:42 * * Public
class Memoryleaktest {
public static void Main (string[] args) {
Vector v = new Vector (100000000);
for (int i=0;i<v.size (); i++) {
Object o = new Object ();
V.add (o);
o = null;}}}
The results of this procedure are as follows:
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap spaces at
java.util.vector.<init> ( vector.java:131) at
java.util.vector.<init> (vector.java:144) at
Cn.com.yy.MemoryLeakTest.main ( MEMORYLEAKTEST.JAVA:13)
Analytical:
In the For loop, loop the object, apply one to the vector, and leave the object blank. At this point, there is a path to the GC roots for object objects because they are referenced by a vector object, but after the object object is empty the object object is useless, so there is a path to the GC roots for all object objects, but it is useless. So it caused heap memory leaks.