Deep understanding of Java Virtual Machine learning notes (2)

Source: Internet
Author: User

This section describes

    • OutOfMemoryError test for Java heap
    • Eclipse Memory Analyzer Analysis of overflow
    • Virtual machine stack and local method stack Stackoverflowerror test
    • Method area and run-time-constant pool overflow
    • Native Direct Memory Overflow

OutOfMemoryError test of Java heap

  (1) First set Debug configuration. As shown in the following:

  (2) The next encoding operation, as shown in the following code, keeps adding new objects to the list. Because the Java heap is set to a size of 20M and is not extensible (you can avoid heap auto-scaling by setting the minimum-xms parameter of the heap to the same as the maximum-xmx parameter), the parameter-xx:+ Heapdumponoutofmemoryerror can cause a memory overflow exception in the virtual machine to dump the current memory heap dump snapshot for analysis afterwards.

Java heap Memory overflow test: As shown below:

  

  (3) Result analysis: Oom (OutOfMemoryError) Anomaly of Java heap memory is a common memory overflow anomaly in practical application. When a Java heap memory overflow occurs, the exception stack information "Java.lang.OutOFMemoryError" is followed by a further hint "Java heap space"

  

Second, Eclipse Memory Analyzer analysis of overflow

   (1) Eclipse installs Eclipse Memory Analyzer.

Now it's 1.2.1, http://www.eclipse.org/mat/downloads.php.

You can also install new software via Eclipse, address http://download.eclipse.org/mat/1.2/update-site/

   (2) Open into the following as follows:

To solve the anomaly in this area, the general approach is to first analyze the dump-dump snapshot through a memory-imaging tool such as Eclipse memory Analyzer, with a focus on verifying that the objects in memory are necessary. This is the first to know whether there is a memory leak or memory overflow.

    

    Most of its features can be seen from.
1. Histogram can list objects in memory, number of objects, and size.
2. The Dominator tree can list the space that the thread is occupying, and the objects under the thread.
3.Top consumers lists the largest object by graph.
4.Leak suspects automatically analyzes the cause of leaks via MA.

(3) This focus is to see the leak Suspects, after the point open can see:

    

       

   (4) Results analysis: In this diagram, we can clearly see that this object collection holds a large number of Yourbeauty object references, which is caused by the memory leak.

Third, virtual machine stack and local method stack Stackoverflowerror test

(1 ) A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the maximum allowed depth for the virtual machine.

(2) throws a OutOfMemoryError exception if the virtual machine cannot request enough memory space in the expansion stack. (3) use the-XSS parameter to reduce the stack memory capacity. Test result: Throws a Stackoverflowerror exception. The output stack depth is correspondingly reduced when an exception occurs.     (4) experiment shows: in single-threaded case, whether because the stack frame is too large or the virtual machine stack capacity is too small, when the memory can not be allocated, the virtual machine throws a Stackoverflowerror exception.

Four, method area and run-time-constant pool overflow

  (1) because the run-time constant pool is part of the method area, the overflow tests for these two areas are put together. As mentioned earlier, JDK 1.7 begins a gradual "go to a permanent generation", where you can test the code to see how it actually affects the program.

    

   (2) String.intern () is a native method that is used if the string constant pool already contains a string equal to this string object, then returns a String object that represents it in the pool; Adds the string contained by this string object to the constant pool, and returns a reference to this string object. In JDK1.6 and previous versions, because of the constant pool allocation in the permanent generation, we can limit the method area size through-xx:permsize and-xx:maxpermsize, thereby indirectly limiting the capacity of the constant pool.

  

V. Native Direct Memory Overflow

  (1) directmemory capacity can be specified by-xx:maxdirectmemorysize, and if not known, the default is the same as the Java heap maximum (-xmx specified), the following code crosses the Directbytebuffer class, Obtain an unsafe instance directly from reflection for memory allocation. Because, although allocating memory with Directbytebuffer also throws a memory overflow exception, it throws an exception when it does not actually request to allocate memory to the operating system, but instead calculates that the memory cannot be allocated, and then throws the exception manually. The real way to apply for allocating memory is unsafe.allocatememory ()

  

(2) Use unsafe to allocate native memory.

  

 

Based on how the GC works, we can make the GC run more efficiently and meet the requirements of the application in a number of tricks and ways. Here are a few suggestions for programming.

1. The most basic suggestion is to release the reference to the useless object as soon as possible. Most programmers use temporary variables to have the reference variable automatically set to null after exiting the active domain (scope). When we use this approach, we must pay special attention to complex object graphs, such as arrays, queues, trees, graphs, etc., which are more complex to refer to each other. For such objects, GC recycling is generally less efficient. If the program allows, the unused reference object will be assigned null as soon as possible. This will speed up the GC's work.

2. Use the Finalize function sparingly. The Finalize function is a chance that Java provides programmers with an opportunity to release objects or resources. However, it will increase the amount of GC work, and therefore minimize the use of a Finalize method to reclaim resources.

3. If you need to use a frequently used picture, you can use the Soft app type. It can save the picture in memory as much as possible for the program to call, without causing outofmemory.

4. Note the collection data types, including arrays, trees, graphs, linked lists and other data structures, which are more complex for GC to recycle. Also, be aware of some global variables, as well as some static variables. These variables tend to cause hanging objects (dangling reference), causing memory waste.

5. When the program has a certain wait time, the programmer can manually execute System.GC () to notify the GC to run, but the Java language specification does not guarantee that the GC will be executed. Using incremental GC can shorten the pause time for Java programs.

Deep understanding of Java Virtual Machine learning notes (2)

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.