"Java" Java memory management

Source: Internet
Author: User

    Java memory Management is a frequently asked question in the interview. In fact, Java memory management refers to the allocation and release of objects. Once read the phrase: "C + + programmers feel that memory management is too important, so must manage themselves, and Java programmers feel that memory management is too important, must not manage themselves." I think that's a very incisive remark. C + + programmers need to explicitly allocate memory and free up memory, which often causes "memory leaks". While Java programmers do not need to explicitly allocate and free memory,Java automatically allocates memory when objects are created and frees memory when objects are no longer in use . Java objects are created with the new keyword, and when created, a memory space (in the heap) is requested for each object, and Java's memory release is determined and executed by the garbage collection mechanism (GC). This memory management mechanism facilitates both the programmer and the ability to reduce memory leaks. However, this can aggravate the JVM's work, so the Java program runs slower.
What is a memory leak? memory leaks are actually some of the objects that are allocated, they are no longer used, but the GC cannot be reclaimed, they will always consume memory. Why the GC cannot be recycled, you should first understand the Java garbage collection mechanism.
Java garbage collection mechanism: When an object is no longer in use, the JVM automatically frees the memory used by the object. The principle of freeing memory is that objects are no longer referenced, and Java uses soft pointers (pointers do not point directly to objects, but rather to references to objects) to track individual references to objects. Here is a concept that we can use as a vertex of a directed graph, consider a reference relationship as an edge of a graph, and have a pointing edge from a reference object to the referenced object. A vertex-accessible object is a valid object, the GC is not recycled, and instead, we think the objects are no longer referenced, and the GC reclaims the objects. This process programmers do not need to participate, can be said to be transparent to the programmer, in general, we do not need to explicitly request the garbage collector, because it is run automatically, it will occasionally check the object's various references, reclaim the memory of no Application object. The system class has a static GC () method that can run the garbage collector, but it does not guarantee that objects can be reclaimed immediately. In addition, the Java language does not necessarily require the JVM to have a GC, and does not stipulate how the GC will work, but the general JVM has a GC, most of which use similar algorithms to manage memory and perform recycling operations. Java's garbage collection mechanism is for all Java applications to serve, so no one process can command what the garbage collection mechanism does and what to do. Specific garbage collection mechanism you can refer to the relevant information.
The reason for memory leaks is " object references that remain but never used " because you forget to "free" previously allocated memory. If the program retains objects that are no longer in use, the automatic garbage collector cannot prove that the objects are no longer in use, and in order to ensure that the object's memory can be reclaimed, the object is usually explicitly set to NULL, or the object is removed from the collection.
    
Author: Li Li
Sign: Life does not have too many illusions, but to have more action.
    

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Java" Java memory management

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.