Java: knowledge about memory and java memory

Source: Internet
Author: User

Java: knowledge about memory and java memory
Memory managed by JVM

  • Program counter (PC): Each thread has an independent PC
  • JVM stack (Java stack): Describes the Memory Model of Java method execution. When each method is executed, a stack frame is createdStore Local variable tables, Operation stacks, dynamic links, and method exitsAnd other information.
  • Local method Stack: related to Native METHOD
  • Java heap: memory address space shared by threads. It is used to store object instances and is also the main area of GC management.
  • Direct Memory: Related to Native heap
  • Method Area: stores information about classes loaded by JVM, constants, static variables, and other data. The memory recycle targets of this area are mainly for collection of constant pools and class uninstallation. The method area is also the memory area shared by threads. Although it is described as a logical part of the Heap, its alias is Non-Heap ).
  • Runtime frequent volume pool: A Part Of The method area. Constant pools store various types generated during the editing period.Literal and symbolic referencesWhen the class is loaded, it is stored in the runtime pool of the method area.
Memory Allocation Rules
  • Objects are preferentially allocated in the Eden area. After Minor GC (New Generation GC), objects are not enough to be placed in the same vor area and enter the old age.
  • Large objects directly enter the Old Age
  • Objects that survive for a long time enter the old age: achieved through the age counter.
  • The sum of VOR objects of the same age is greater than half of the space. Objects older than this age enter the old age.
  • To determine whether the objects in the old age are larger than the remaining space in the old age, Full GC (GC in the old age) is performed ).
GC collection

PC, Java stack, local method stack along with the thread and method life cycle, so the memory allocation and recovery are deterministic.
The Java heap and method area can only know the object to be created during runtime. The memory allocation and recovery are dynamic, and GC focuses on the memory. PassRoot Tracing MethodThat is, a reference chain is used to determine whether the object is alive and whether to recycle the object (reference counting cannot solve the problem of object mutual reference ). The method area mainly recycles discarded constants and useless classes.

Garbage collection algorithm:

  • Mark-clearing algorithm (Mark-Sweep)
  • Copy algorithm (Copying): divides the memory into a large Eden space and two smaller shard vor spaces, and copies Eden and one of the shard vor surviving objects to another shard vor space, finally, the first two spaces are recycled. When the primary vor space is insufficient, the old storage space is allocated for guarantee. Most of the new generation objects are born and died, so this algorithm is often used to recycle the new generation.
  • Mark-Compact: moves all surviving objects to one end and clears objects other than the boundary of other ends.
  • Generation-based collection: different memory segments are divided into several parts based on the object survival cycle, which are generally divided into the Young and Old generations. Different collection algorithms are used for different ages. For example, a replication algorithm is used for the new generation, and a tag clearing or tag sorting and recycling algorithm is used for the old generation.

A virtual machine can have multiple different GC types. For example, HotSpot has seven GC Types acting on different generations. In addition, the number of GC threads (single thread or multi-thread) may be different.

Java Memory Model

JVM tries to define a memory model to avoid memory access differences between various hardware and operating systems, so as to achieve consistent concurrency on each platform.

Mutual memory operations:

  • Lock
  • Unlock
  • Read
  • Load
  • Use
  • Assign
  • Store
  • Write

When a variable is definedVolatileThere are two special effects: visibility and no re-sorting. Visibility means that, although atomic operations are performed, other threads do not necessarily see the latest modified value. Visibility ensures that the latest value has been refreshed to the main memory. The re-sorting means that the order of variable assignment operations cannot be the same as that of code execution. If you observe in this thread, all operations are ordered, that is, there is a serial semantics in the thread; if you observe another thread in one thread, the operation may be out of order due to Command Re-sorting.

Refer:
Deep understanding of Java Virtual Machine
Http://www.th7.cn/Program/java/201409/276272.shtml

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.