"Go" Java Learning---The difference between memory leaks and overflow

Source: Internet
Author: User
Tags xms

The difference between Java memory leaks and overflow

Java memory leak is not in time to clean up the memory garbage, causing the system can no longer provide you with memory resources (memory resource exhaustion);

And the Java memory overflow is that you asked to allocate more memory than the system can give you, the system can not meet the requirements, and then overflow.

Memory overflow similar to array, exceeding the upper limit of data you can store

Memory leak, that is, after the memory has been used, cannot release the Recycle reuse

the difference between Java memory leaks and overflow

Memory overflow is the memory you require to allocate more than the system can give you, the system can not meet the demand, so overflow.

A Java memory leak is a failure to clean up the memory garbage in time, causing the system to no longer provide you with memory resources (memory resource exhaustion).

See the above explanation, some friends may still not understand it. No problem, see the following example

1.Java memory leak is a program logic problem, resulting in the application of memory can not be released. In this case, no matter how much memory, sooner or later will be occupied by light.

The simplest example is the cycle of death. This is often the case due to procedural errors.

A 2.Java memory leak means that the allocated memory on the heap is not released, thereby losing control of it. This will cause the program to use less memory, causing the system to slow down, the serious situation will make the program out.

3. There is a bit of a discrepancy in memory overflow. For example, you apply for an integer, but give it a long to save the number, that is, memory overflow.

To give a real-world example:

For example, there is a bucket filled with water. You throw an apple in. The water in the pail is normal. If you put a big stone. Water overflows, and memory overflows are the principle.

Difference: memory overflow, insufficient memory provided, Java memory leak, no more memory resources available

Perhaps you will ask the memory leaks and overflow is the test of Java? What's the use of this test?

My personal feeling is to test everyone's knowledge of how to manage memory in Java? The bottom is the explanation of how Java is managing memory

How Java is managing memory

To determine if there is a memory leak in Java, we must first understand how Java manages memory. The memory management of Java is the allocation and release of objects. In Java, the allocation of memory is done by the program, and the release of memory is done by the garbage collector (GARBAGECOLLECTION,GC), and the programmer does not need to call the function to free memory, but it can only reclaim the space occupied by those objects that are useless and are no longer referenced by other objects.

The memory garbage collection mechanism of Java is to check the reference chain from the main running object of the program, and when it is traversed, it is found that there are no referenced orphaned objects as garbage collection. In order to properly dispose of objects, the GC must monitor the running state of each object, including the application, reference, reference, assignment, etc. of the object, which the GC needs to monitor. The object's state is monitored to release the object more accurately and in a timely manner, and the fundamental principle of releasing the object is that the object is no longer referenced.

In Java, these useless objects are collected by the GC, so programmers do not need to consider this part of the memory leak. Although we have several functions that can access the GC, such as the function System.GC () that runs the GC, the function does not guarantee that the JVM's garbage collector will execute, as defined by the Java language Specification. Because different JVM implementations may use different algorithms to manage the GC. Typically, GC threads have a lower priority level. There are a number of policies that the JVM calls the GC, some of which are used to a certain extent, the GC starts to work, there are timed executions, there is a gentle execution of the GC, and some interrupt-execution GC. But generally speaking, we don't need to care about this.

This exception information is thrown in the JVM if 98% of the time is used for GC and the available heap size is less than 2%.

The initial memory allocated by the JVM is specified by-XMS, which defaults to 1/64 of the physical memory;

The maximum allocated memory for the JVM is specified by-XMX, which defaults to 1/4 of the physical memory.

The maximum value of the JVM memory is very much related to the operating system. 32-bit processor Although the controllable memory space has 4GB, but the specific operating system will give a limit, this limit is generally 2GB-3GB (generally speaking under the Windows system for the 1.5g-2g,linux system is 2g-3g), and more than 64bit processors will not be limited.

Note: If XMS exceeds the XMX value, or if the sum of the heap maximum and the non-heap maximum exceeds the physical memory or the maximum operating system limit, the server will not start up.

Common scenarios for this error:

A) When uploading files to the Web.

b) Open large files or fetch too much data from the database at one time.

"Go" Java Learning---The difference between memory leaks and overflow

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.