性能问答>关于不同虚拟机版本下的-XX:+TraceClassUnloading>
8回复
5年前

关于不同虚拟机版本下的-XX:+TraceClassUnloading


  1. 在JDK8中尝试重现类卸载的例子, 加入该参数并无法看到控制台有对应的unload日志输出(macos环境jdk1.8.0_161, linux环境jdk1.8.0_91均有尝试);
  2. 由于TraceClassUnloading在JDK9后不建议使用了, 被替换为了-Xlog:class+unload=info。 同样的代码在JDK9(macos环境make编译), JDK11下, 在新参数下, 均能看到对应的unload日志。形如
    [0.106s][info][class,unload] unloading class com.github.universe.java.base.Pow 0x0000000800060840
  3. 为何?

测试代码如下:

public class Loop {


	public static void main(String[] args) throws Exception{

		ClassLoaderB loaderB = new ClassLoaderB("CLB");
		loaderB.setPath("/Users/mozilla/core/universe/java-base/target/classes/");
		Class<?> clazz = loaderB.loadClass("com.github.universe.java.base.Pow");
		Object object = clazz.newInstance();
		System.out.println(object);

		System.out.println("-----------------");
		loaderB = null;
		clazz = null;
		object = null;

		System.gc();

		// System.gc();
		while (true){
			Thread.sleep(100000);
		}
	}

	private static class ClassLoaderA extends ClassLoader{}
	private static class ClassLoaderB extends ClassLoader {

		private String classLoaderName;

		//类的扩展名
		private final String fileExtension = ".class";

		private String path;

		public void setPath(String path) {
			this.path = path;
		}

		public ClassLoaderB(String classLoaderName) {
			super();
			this.classLoaderName = classLoaderName;
		}

		public ClassLoaderB(String classLoaderName, ClassLoader parent) {
			super(parent);
			this.classLoaderName = classLoaderName;
		}


		@Override
		protected Class<?> findClass(String className) throws ClassNotFoundException {
			System.out.println("findClass invoked: " + className);
			System.out.println("class loader name: " + this.classLoaderName);
			byte[] data = this.loadClassDate(className);
			return this.defineClass(className, data, 0, data.length);
		}

		private byte[] loadClassDate(String name) {
			InputStream is = null;
			byte[] data = null;
			ByteArrayOutputStream baos = null;

			try { 
				name = name.replace(".", "/");
				is = new FileInputStream(new File(this.path + name + this.fileExtension));
				baos = new ByteArrayOutputStream();

				int ch = 0;
				while ((ch = is.read()) != -1) {
					baos.write(ch);
				}
				data = baos.toByteArray();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					is.close();
					baos.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return data;
		}
	}
}

6217 阅读
请先登录,查看8条精彩评论吧
快去登录吧,你将获得
  • 浏览更多精彩评论
  • 和开发者讨论交流,共同进步