What is the reason for Java OutOfMemoryError and what is the Java native method

Source: Internet
Author: User

What is the reason for Java OutOfMemoryError and what is the Java native method?

Second, the Java native method reproduced Blog

First article:

It took two hours today to get a good read of the English article about what native method is, and here's what I understand in terms of the original text.

2.1 What is native Method
In a nutshell, a native method is a Java interface that calls non-Java code. A native method is a Java approach: the implementation of this method is implemented by non-Java languages, such as C. This feature is not specific to Java, many other programming languages have this mechanism, such as in C + +, you can use the extern "C" to tell the C + + compiler to call a C function.
"A native method is a Java method whose implementation was provided by Non-java code."
When defining a native method, the implementation body is not provided (some like the definition of a Java interface) because its implementation is implemented outside the non-Java language. , an example is given below:
public class Ihavenatives
{
Native public void Native1 (int x);
Native static public long Native2 ();
Native synchronized private float Native3 (Object o);
native void Native4 (int[] ary) throws Exception;
}
The declarations of these methods describe what the non-Java code looks like (view) in these Java code.
The identifier native can be used with all other Java identifiers, except for abstract. This is justified because native implies that these methods are implemented, except that the implementations are non-Java, but the abstract clearly indicates that these methods are not implemented. Native and other Java identity Connect prompt time, its meaning is not the same as the non-native method, such as native static shows that this method can not produce instances of the class directly called, which is very convenient, such as when you want to use a native method to invoke a C class library. The third method above uses the native SYNCHRONIZED,JVM to perform a synchronous lock mechanism (like multithreading in Java) before entering the implementation of this method. )
A native method can return any Java type, including non-basic types, and can also be used for exception control. The implementation of these methods can make an exception and throw it, which is very similar to the Java approach. When a native method receives some non-primitive types such as Object or an integer array, this approach can access this non-basic type of interior, but this will make the native method dependent on the implementation of the Java class you are accessing. One thing to keep in mind is that we can access all Java features in a local implementation of a native method, but that depends on the implementation of the Java features you're accessing, and it's far less convenient and easy to use those features in the Java language.
The presence of the native method does not have any effect on other classes calling these local methods, and the other classes that actually call these methods do not even know that it is calling a local method. The JVM controls all the details of calling the local method. It is important to note that when we declare a local method as final. The method body implemented in Java can be compiled with increased efficiency due to inline. However, it is doubtful whether a native final method can get such benefits, but this is only a problem with the optimization of the code and has no effect on the implementation of the function.
If a class with a local method is inherited, the subclass inherits the local method and can rewrite the method in the Java language (this may seem strange), as well if a local method is fianl identified, it cannot be overridden after it is inherited.
Local methods are very useful, Because it effectively expands the JVM. In fact, the Java code we write has already used native methods, and in the implementation of the Concurrency (multithreading) mechanism of Sun's Java, many of the contact points of the operating system have been used locally, which allows the Java program to go beyond the Java runtime boundaries. With local methods, Java programs can do any application-level tasks.


Two. Why use native Method
Java is very convenient to use, but some levels of tasks are not easy to implement in Java, or we are concerned about the efficiency of the program, the problem comes.
Diplomacy with the Java environment:
Sometimes Java applications need to interact with environments outside of Java. This is the main reason why the local method exists, and you can think about how Java needs to exchange information with some underlying systems, such as the operating system or some hardware. The local approach is one such communication mechanism: it provides us with a very concise interface, and we do not need to understand the tedious details outside of Java applications.
Interacting with the operating system:
The JVM supports the Java language itself and the runtime library, which is the platform on which the Java program survives, consisting of an interpreter (the explanatory bytecode) and some libraries connected to the local code. However, after all, it is not a complete system, it often relies on some of the underlying (underneath) system support. These underlying systems are often powerful operating systems. By using the native method, we were able to implement the JRE's interaction with the underlying system in Java, and even some parts of the JVM were written in C, and if we were to use some of the Java language itself without providing the features of the encapsulated operating system, we would also need to use local methods.
Sun ' s Java
Sun's interpreter is implemented in C, which allows it to interact with the outside like some normal C. Most of the JRE is implemented in Java, and it interacts with the outside world through some local methods. For example, the SetPriority () method of the class Java.lang.Thread is implemented in Java, but it implements the invocation of the Local Method SetPriority0 () in the class. This local method is implemented in C and is implanted inside the JVM, and on the Windows 95 platform, this local method will eventually invoke the Win32 setpriority () API. This is a specific implementation of a local method that is provided directly by the JVM, and more generally, the local method is provided by the external dynamic link library (external. dll) and then called by the JVM.


Three. How the JVM makes native method run:
We know that when a class is first used, the byte code of this class is loaded into memory and is only reloaded once. The entrance to the loaded bytecode maintains a list of all the method descriptors for that class, which contain information such as where the method code is stored, what parameters it has, the method's descriptor (public, and so on), and so on.
If there is a native in a method descriptor, this description converts sequential blocks will have a pointer to the implementation of the method. These implementations are within some DLL files, but they are loaded into the Java program's address space by the operating system. When a class with a local method is loaded, its associated DLL is not loaded, so pointers to the implementation of the method are not set. These DLLs are not loaded until the local method is called, which is implemented by calling Java.system.loadLibrary ().

The last thing to note is that the use of local methods is expensive, and it loses many of the benefits of Java. If there is no choice, we can choose to use local methods.


What is the reason for Java OutOfMemoryError and what is the Java native method

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.