java程序运行的时候,是把所有的class文件都加载到内存吗?还是用的什么加载什么?
高洛峰
高洛峰 2017-04-18 10:49:58
0
4
540

一运行就全部加载?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
洪涛

Not all loading, it can be understood as loading on demand.
For example, the inherited parent class, the implemented interface, the declared variables, the return type of the method, etc.

That is, when using the current class, any necessary classes that are missing will be loaded immediately.

Peter_Zhu

Load on demand.
Timing of class loading:

1) When encountering the four bytecode instructions of new, getstatic, putstatic or invokestatic, if the class has not been initialized, its initialization needs to be triggered first. The most common Java code scenarios that generate these four instructions are: when using the new keyword to instantiate an object, reading or setting a static field of a class (modified by final, and the result has been put into the constant pool at compile time). fields), and when calling a static method of a class.
2) When using the method of the java.lang.reflect package to make a reflective call to a class, if the class has not been initialized, its initialization needs to be triggered first.
3) When initializing a class, if it is found that its parent class has not been initialized, you need to trigger the initialization of its parent class first.
4) When the virtual machine starts, the user needs to specify a main class to be executed (the class containing the main() method), and the virtual machine will first initialize the main class.
5) When using the dynamic language support of JDK 1.7, if the final parsing result of a java.lang.invoke.MethodHandle instance is the method handle of REF_getStatic, REF_putStatic, REF_invokeStatic, and the class corresponding to this method handle has not been initialized, then Its initialization needs to be triggered first.
For these five scenarios that will trigger the initialization of a class, the virtual machine specification uses a very strong qualifier: "yes and only". The behavior in these five scenarios is called active reference to a class. In addition, all methods of referencing a class will not trigger initialization and are called passive references.

Source: In-depth understanding of JVM 7.2 class loading timing

巴扎黑

I haven’t fully understood this area yet, so I don’t dare to mislead others.
I know that when the virtual machine is running, it will preload a commonly used class, such as the one under the java.lang package.
As for the class files/jar packages you reference in the program, there is a loading process.
When the virtual machine needs a class file, it will call the default classLoader.loadClass() to load it. This method first searches the loaded class. If it is found, it will of course be returned. If it is not found, it will be handed over to the parent loader. After searching layer by layer, classLoader will call the findClass method to load the file.
protected Class<?> loadClass(String name, boolean resolve)

    throws ClassNotFoundException
{
    synchronized (getClassLoadingLock(name)) {
        // First, check if the class has already been loaded
        Class<?> c = findLoadedClass(name);
        if (c == null) {
            long t0 = System.nanoTime();
            try {
                if (parent != null) {
                    c = parent.loadClass(name, false);
                } else {
                    c = findBootstrapClassOrNull(name);
                }
            } catch (ClassNotFoundException e) {
                // ClassNotFoundException thrown if class not found
                // from the non-null parent class loader
            }

            if (c == null) {
                // If still not found, then invoke findClass in order
                // to find the class.
                long t1 = System.nanoTime();
                c = findClass(name);

                // this is the defining class loader; record the stats
                sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                sun.misc.PerfCounter.getFindClasses().increment();
            }
        }
        if (resolve) {
            resolveClass(c);
        }
        return c;
    }
伊谢尔伦

http://blog.csdn.net/briblue/...
You can refer to this blog, it’s very good

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!