Java technology topic-JVM-is your memory leaked?

Source: Internet
Author: User

I. Starting from an example about JVM Memory leakage, let's start with the following example. Let's determine if there will be Memory leakage according to the following allocation method? 1 import java. util. list; 2 import java. util. vector; 3 4 public class MemoryTest {5/** 6 * @ param args 7 */8 public static void main (String [] args) {9 // TODO Auto-generated method stub10 Vector vobj = new Vector (10); 11 for (int I = 0; I <100; I ++) 12 {13 Object obj = new Object (); 14 vobj. add (obj); 15 // note how Java clears the memory 16 obj = null; 17 // does GC clear the memory? 18} 19 System. out. print ("end"); 20} 21} The above program does not have any exception prompts, does it mean that there is no memory leakage. What if I set the maximum value to 100000000 at this time? Run the program by yourself. The following is the result of my actual test. It depends on the specific JVM parameter settings. The limit values of each person may be different. 1 Exception in thread "main" java. lang. outOfMemoryError: Java heap space2 at java. util. arrays. copyOf (Unknown Source) 3 at java. util. arrays. copyOf (Unknown Source) 4 at java. util. vector. ensureCapacityHelper (Unknown Source) 5 at java. util. vector. add (Unknown Source) 6 at MemoryTest. main (MemoryTest. java: 14) Memory leakage. However, this infinite amplification method can only be found after the event, the event has come out, and the production application should be affected. Therefore, detection afterwards does not help. We also need to find another way. Ii. How to determine whether memory leaks the JVM virtual machine memory structure the previous blog has explained that as long as there is still available space in the Java heap, no problem will occur even if it is leaked, unless the memory space has exceeded the JVM memory settings. If the memory limit is not set and the memory space is large, no error is prompted. This has brought me troubles. Do I have to wait for the memory to be insufficient to discover Memory leakage only when an exception occurs? None. (1) code review: high technical requirements, pure empirical method, high possibility of omission, strong operability, but generally require mutual review. (2) tool 1: Output jdk gc log data: Set: JAVA_OPTS Add the following parameters to open the jdk GC output log:-verbose: gc-XX: + PrintGCDetails-XX: + HeapDumpOnOutOfMemoryError (3) jmap: jmap-dump: format = B, file = jmap. hprof <PID> (4) Eclipse Memory Analyzer: professional tools are required to import jmap. hprof file, link address attached, detailed not to mention, so that there is no suspicion of advertising.

Related Article

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.