Java GC Memory

Source: Internet
Author: User

1.JAVA GC Mechanism

GC primarily reclaims heap memory: turn from 51375662

Heap Memory: Stores objects and arrays in Java, new objects, queue FIFO, run-time dynamically allocated size

Stack memory: Mainly used for the execution of programs, such as basic type variables and objects of the Application object, stack advanced out, access speed, second only to register, stack data can be shared. The disadvantage is that the size of the stack data and the life cycle must be deterministic.

Stack memory is a first-level cache and can be reclaimed by Java GC

Here are some of the actual writing procedures should be noted in the process of the points, to develop these habits can be to some extent reduce the unnecessary consumption of memory, further can be reduced because of insufficient memory to cause the GC to continue. A similar experience can accumulate more communication:

    1. Reduce the new object. After each new object, a new memory space is opened. After these objects are not referenced, they are also recycled. Therefore, if you maximize the reasonable reuse of objects, or use basic data types to replace objects, can help save memory;
    2. Use more local variables to reduce the use of static variables. Local variables are created in the stack, and the access speed is fast. Static variables are in heap memory;
    3. Avoid using finalize, which adds a lot of burden to the GC;
    4. If it is a single thread, use non-multithreaded security as much as possible, because thread safety comes from the synchronization mechanism, which degrades performance. For example, a single-threaded application, you can use HashMap, do not use Hashtable. Similarly, minimize the use of synchronized
    5. Replace the multiplication number with a shift symbol. Eg:a*8 should write a<<3
    6. Use caching for objects that are often reused;
    7. Use a single-dimensional array instead of a two-dimensional array as much as possible using the base type instead of the wrapper type;
    8. Use final modifier as far as possible, final indicates non-modifiable, high access efficiency
    9. Single-threaded case (or for local variables), the string as far as possible to use StringBuilder, faster than StringBuffer;
    10. Why is string slow? Because string is an immutable object, each time a change is made to the string type it is equivalent to generating a new string object and then pointing the pointer to the new string object. If thread safety is not guaranteed, try to use StringBuffer to concatenate strings. It is important to note that the default cache capacity of StringBuffer is 16 characters, and if more than the 16,apend method calls the private Expandcapacity () method, sufficient cache capacity is guaranteed. Therefore, if you can preset stringbuffer capacity, avoid append to expand capacity. If you can guarantee thread safety, use StringBuilder. Examples of the following two examples •:

Java GC Memory

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.