JVM memory Leak Simple example __JVM

Source: Internet
Author: User
Tags object object

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.


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.