简体   繁体   中英

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? If flag is false will class D be loaded?

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

A and Object as a minimum. Depending on the implementation, the JVM might load B and C on class initialisation or when you create your first instance. C might not be initialised until m2() is called.

in which memory location in JVM these objects would be put

Small objects are placed in the Eden space regardless of which ClassLoader you use.

If flag is false will class D be loaded

Probably not but it depends on the JVM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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