JVM, Java Virtual Machine Basic Knowledge Tutorial

Source: Internet
Author: User
Keywords java virtual machine tutorial jvm virtual machine java basic tutorial
1. Summary

First of all, this article does not involve principles, but explains my own experience based on the author's "In-depth Understanding of the Java Virtual Machine" several times. From a novice to now, I understand what JVM is, how to understand and understand, Explain such an experience only. This article does not dig deep into JVM, and currently does not have this ability. It just uses a familiar and easy-to-understand way to let readers understand what JVM is. Let me start my explanation.


Alibaba Cloud Simple Application Server:  Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.


2. Java cross-platform
  Understanding the cross-platform features of Java is the most intuitive understanding of JVM. The so-called "compile once, run everywhere", why can't C/C++ be realized? This type of language directly uses the physical hardware (or the memory model of the operating system), so the memory model between different systems is different, such as Linux and Window, which means that the code compiled in Window cannot be Run on Linux. "In-Depth Understanding of the Java Virtual Machine" records that the Java Virtual Machine specification attempts to define a Java Memory Model (JMM) to shield the memory access differences of various hardware and operating systems, so that Java programs can be used on various platforms. A consistent concurrency effect can be achieved. To give a realistic example, how can a person who only hears Chinese communicate with a person who only hears English? In the Java world, the method adopted is to assign a translator to each person on both sides ( JVM), so this is why JVM must have a window version and a Linux version.

   As we all know, the final appearance of Java program compilation is a .class file, and the translation results of each .class file of different virtual machines are consistent. For C/C++, the compiler generates pure binary machine instructions that directly face the memory of the computer system. However, the compiled result of the java program is for the JVM and is to be delivered to the JVM for further processing. So that the computer can recognize and run, which is the so-called "shielding the memory access differences of various hardware and operating systems." The characteristics here have an indescribable relationship with the object-oriented interface, I just need to have such a specification, and I don’t need to know the underlying principle of contacting you.

3. Virtual Machine
   JVM, the full name is Java Virtual Machine, English is Java Virtual Machine, a brief discussion of the three words virtual machine, it is also very comfortable for later learning. Baidu Baike describes that "Virtual Machine (Virtual Machine) refers to a complete computer system with complete hardware system functions simulated by software and running in a completely isolated environment", but the essence of the virtual machine is still a process of the computer system. Analogous to Hong Kong and Macao, they have a high degree of autonomy, but in essence they still belong to China. For the convenience of description, we regard the entire computer as a building, and the virtual machine as a certain floor. The building divides an area to a floor, and lets this floor manage itself, which corresponds to that the computer divides a memory for the JVM, and the JVM manages itself. The following picture shows the memory usage on the author's Aliyun server. You can see that the JVM takes up nearly 500M of memory. So the question is, what does the JVM need so much memory for, and how is it divided?



4. Memory
At this stage, no matter what programming language is designed and invented by humans, programming language is one of the tools for humans to achieve their goals. Therefore, programming language seems to be a brand new thing, but it can be everywhere. Seeing the shadow of human thought, you can find similar examples in reality.

   Following the example above, the JVM is used as a certain floor, which alone occupies a large area (memory). On this floor, there are the main hall, living room, and guest room. One of the four differences is fine. The main hall and the living room are public, while the main room and guest rooms are private. This corresponds to the JVM level. Certain pieces of memory, no matter who visits it or how many people (threads) visit it, these shared areas can serve them, while guest rooms and private areas, assuming our floor is relatively strong, we will see how many guests come. Build each guest room separately for them, each guest has its own private area, guest A cannot enter guest B’s room.

   The corresponding JVM level, that is, the JVM runtime data area is divided into two blocks, the thread isolation area and the thread shared area. We can simply understand that each guest is a thread. How is the specific corresponding area divided, and what role does it have, here can only be understood and remembered conceptually, and the portal can easily understand the JVM runtime data area here

5. What is rubbish
Recalling that when I was a child, many friends would come to my house to play together. Although everyone had a great time, they would pat their buttocks and leave after the time point, leaving my house in a mess. At this time, if I don’t clean up the garbage. If so, my mother is probably coming to pat my ass. The same is true for guests (threads). I have been in your world and will always leave traces that belong to me. Whether it is rubbish or not depends on how you decide. Here comes another question, how do I judge which is rubbish and which is not rubbish, it's just that when I play, I or my friend throws my mother's ring on the ground. From the perspective of humans, you can know at a glance which is garbage and which is not garbage, but computers are not like that. Computers are sometimes inferior to humans. It is clear that humans can accomplish things in one glance or in a few seconds, and computers must also be able to do the same. If the function of the device, you need to pay tens of millions of times the price to be able to achieve.

When the author’s mother cleans up the trash at home, in order to ensure that the thrown away is trash, she will ask everyone in the family one by one if this item is yours, do you want it, when there is no one When he admits that this thing belongs to him, my mother treats this thing as rubbish. When someone says this is not rubbish, it is his treasure, my mother will mark this thing. The JVM adopts a similar approach. Each object has a certain connection and path reachability to the GC ROOT. When an object is unreachable to the GC ROOT (that is, no one says this thing belongs to him), the JVM It is judged as garbage. This behavior is called reachability analysis in the JVM.

The role of finalize: Suppose that a notebook has been recognized by my mother as garbage, but when my mother throws away the garbage, she must go through a procedure, namely finalize. If in the process of this procedure, I suddenly remembered that this notebook is still useful to me Yes, then this notebook will not be recognized as garbage and will continue to be kept in memory.

6. Garbage Collection Algorithm
   knows what garbage is, and found the location of the garbage, the next question is how do I deal with this garbage, namely garbage collection. How do I forget who you are? The key is how to write and how this action happened.

Mark removal: On this floor, rubbish is everywhere, even scattered among or around non- rubbish, even if my mother has made a mark for useful things, then this means that my mother only needs to remove those that are not. Logo things. The visual achievement of the practice of mark removal is that my mother took the trash can from beginning to end, and threw him into the trash can when she saw it. This approach is undoubtedly the easiest, but the consequences are obvious, too much space debris. How to understand the space debris here, each item (each object) needs to occupy a certain area (ie memory), he must stand on his heels, but if the space debris is too much, it will cause large When the item comes, the area (memory) is not enough, it will trigger garbage collection again (meaning you may have to pause for a few seconds when you play the game). To give another example in reality, when packing the suitcase, place things casually and randomly. The suitcase is easily filled up. At this time, if you want to put a pair of shoes in, you will find that the space is not enough. Pour out all the things neatly and place them from left to right from top to bottom. For the utilization of space, the neat approach is higher than the unorganized behavior, which leads to another approach, the tag sorting algorithm.

   Marking and sorting: When you have seen someone sweeping the floor, you will sweep the trash directly into the broom, and sweep it when you see it. nonexistent! People are inert, and all people will always find a more efficient way. The more accustomed way is to sweep the garbage together and then throw it into the trash.

   Copy: This algorithm is a bit extravagant. No matter how much rubbish there is in this memory, my mother will throw them away, and then buy the items we still need to use again, commonly known as copy. Therefore, if there are too many surviving objects, this algorithm is not suitable (think about it -.-). Second, the JVM needs to divide the available memory into two halves, half for current use, and half for copied objects use.
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.