A brief overview of the Java language and its garbage collection mechanism

Source: Internet
Author: User

I. overview of the Java language

The Java language is a purely object-oriented programming language that absorbs the advantages of the C + + language. Also rejected the C + + difficult to understand the multi-inheritance, pointers and other concepts so the Java language has a powerful and easy to use two features.

Several important concepts of the Java language are as follows:

J2ME: mainly for the control of mobile devices and information appliances and other limited storage devices

J2SE: the Core and foundation of the entire Java technology,

The most widely used part of J2ee:java technology, it provides a complete solution for enterprise application development.

API: Core Class Library

JRE: A collection of environments necessary to run a Java program, including the JVM standard implementation and the Java Core class library. is the Java platform on which applications can be run, tested and transmitted .

JDK: It is the Java Language Software Development Toolkit, primarily for mobile devices, Java applications on embedded devices ,the JDK is the core of the entire Java development, contains Java's runtime environment,Java Tools, and Java 's base class library.

Java Language is a special high-level language, it has both the characteristics of interpreted language, but also has the characteristics of the compiled language, because the Java program to be compiled first, after parsing two steps.

Compiled language refers to the use of specialized compilers, for a specific platform, a high-level language source code translation into

The machine code (including machine designations and operands) that can be executed by the platform hardware is packaged into an executable program format that can be identified by the platform, which is known as compilation, and the generated executables can be run independently of the development environment and on a specific platform. Since the compiled language is a one-time compiled machine code, it can be run independently from the development environment, but the compiled language program is compiled into machine code on a particular platform, so the compiled executable program is usually not ported to other platforms, and if porting is required, the source code must be copied to the specific platform. Modifications to a specific platform need to be recompiled at least by a compiler on a specific platform.

An interpreted language is a language that uses a specialized interpreter to interpret a source program on a per-line basis into machine code on a particular platform and execute it immediately. An interpreted language is equivalent to mixing the compilation and interpretation processes in a compiled language together. It can be thought that every program that executes an interpreted language needs to be compiled once, so that the program of the interpreted language is usually less efficient and can not run independently from the interpreter. However, the explanatory language has an advantage, the cross-platform is easier to provide only the specific platform of the interpreter, interpretation of the language can easily achieve the source program-level porting, but this is at the expense of the execution efficiency of the program.

The Java language is special, a program written by the Java language needs to be compiled, but this step does not generate the machine code for a particular platform, but rather generates a platform-independent byte code. Of course, this bytecode is not executable and must be interpreted using the Java interpreter. Therefore , the Java language can be considered as both a compiled language and an interpreted language. the Java program must be compiled and explained in two steps first.

In the Java language, the Java Virtual Machine is responsible for interpreting the execution of bytecode files , and the JVM on different platforms is different. However, the JVM on all platforms provides the same programming interface to the compiler, and the compiler only needs to target the virtual machine, generate the code that the virtual machine understands, and then interpret the execution by the VM. the JVM is actually a converter. the uniform standards for the JVM are as follows:

(1) Specify set

(2) Register device

(3) The format of the class file

(4) Stack

(5) garbage collection Heap

(6) Storage area

The purpose of these specifications is to provide a unified standard that ultimately implements the platform independence of the Java program.

Java programs are purely object-oriented languages, soJava programs must exist in the form of classes, which are the smallest program units of a Jave program. Java programs do not allow executable statements, methods, and so on independently, and all program parts must be defined in the class. For a class to be interpreted by the interpreter, the class must contain the main method, and The main method must be fixed. For a large Java program, a single entry is often required, and only one class contains the main method, while the other classes are used to be called directly or indirectly by the main method. The Java virtual machine will only choose to execute from the main method.


two . Overview of Java garbage collection mechanism

the traditional programming language C,C + +, requires the programmer to reclaim the allocated memory. The garbage collection shown is always difficult because programmers do not always know when memory should be freed. If some of the allocated memory is not recovered in time, it will cause the system to slow down, or even cause the program to crash, this phenomenon is called memory leak. the memory allocation and recycling of Java programs is performed automatically by the JRE in the background. the JRE reclaims memory that is no longer in use, and typically the JRE provides a background thread for detection and control, typically garbage collection automatically when the CPU is idle or out of memory.

Java 's heap memory is a run-time data area that holds instances of classes. The heap memory of a Java virtual machine stores all objects established by a running application, in general, garbage Collection is responsible for the collection of heap memory, and all JVM implementations have a heap memory managed by the garbage collector. Garbage collection is a dynamic storage management technology, which automatically frees objects that are no longer referenced by the program, and implements the automatic recovery of memory resources according to the specific garbage collection algorithm.

in the in Java, when no reference variable points to the memory that was originally assigned to an object, the memory becomes garbage. When an object is no longer referenced, the memory reclaims the space it occupies so that the space is later used by the new object. In addition, garbage collection can also erase memory-logged fragments, which are fragmented due to the memory space created by the object and the garbage collector to free up discarded objects. Fragmentation is an area of free memory that is allocated to an object's memory block, and defragmentation refers to moving the heap memory occupied by the Shard to one end of the heap,which the JVM allocates to the new object.

garbage collection can automatically free up memory space and reduce the burden of programming. It can improve programming efficiency and protect the integrity of the program. One drawback of it is that its overhead affects program performance. the Java virtual machine must keep track of all objects in the program before it can determine which objects are useless and eventually dispose of those useless objects. This process takes the processor's time. The second is the incompleteness of the garbage collection algorithm, which does not guarantee that 100% collects all the discarded memory. Any garbage collection algorithm generally has to do two basic things: Find useless objects, reclaim the memory space occupied by the useless objects, so that the space can be reused by the program.

features of the garbage collection mechanism :

(1) can only reclaim memory resources, free memory space in the heap memory, other physical resources such as database connection, disk I/O powerless;

(2) to allow the garbage collection mechanism to reclaim objects that are no longer used, you can set the object's reference variable to null.

(3) The unpredictable nature of garbage collection occurs. Because different JVMs have different garbage collection mechanisms and different garbage collection algorithms, this can happen either regularly or when the CPU is idle, or when memory consumption is at its limit.

(4) The accuracy of garbage collection includes: one is able to accurately mark the Living object, the second is able to accurately locate the reference relationship between objects.

when writing Java Programs, a basic principle is: do not refer to objects that are no longer needed, and if you keep references to those objects, the garbage collection mechanism will not reclaim the object for the time being. can lead to less system memory, and when system memory becomes smaller, garbage collection executes more frequently, resulting in degraded system performance.


A brief overview of the Java language and its garbage collection mechanism

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.