简体   繁体   English

Java类加载器和内存管理

[英]Java classloader and memory management

When the classloader loads class A , which classes will be loaded by the classloader and in which memory location in the JVM would these objects be put? 当类加载器加载类A ,哪些类将由类加载器加载,而JVM中的内存位置是否会放置这些对象? If flag is false will class D be loaded? 如果flagfalse则会加载D类吗?

public class A {
  B b = new B();
  C c = null;
  static int i;
  int j;

  public static void main(String args[]) throws Exception {
    boolean flag = true;
    if (flag) {
        m1();
    }
    A a = new A();
    a.m2();
  }

  private static void m1() {
    D d = new D();
    d.print();
  }

  private void m2() {
    c = new C();
    System.out.println("inside m2");
  }

  private static void m3() {
    System.out.println("inside m3");
  }
}

Which all classes will be loaded by classloader 哪个类将由classloader加载

A and Object as a minimum. AObject作为最低限度。 Depending on the implementation, the JVM might load B and C on class initialisation or when you create your first instance. 根据实现,JVM可能会在类初始化时或在创建第一个实例时加载BC C might not be initialised until m2() is called. 在调用m2()之前,可能不会初始化C

in which memory location in JVM these objects would be put 在JVM中的内存位置将放置这些对象

Small objects are placed in the Eden space regardless of which ClassLoader you use. 无论您使用哪种ClassLoader,都会在Eden空间中放置小对象。

If flag is false will class D be loaded 如果flag为false,则加载D类

Probably not but it depends on the JVM. 可能不是,但它取决于JVM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM