java sun.reflect.GeneratedSerializationConstructorAccessor 类是如何生成的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16708894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:44:21  来源:igfitidea点击:

How the sun.reflect.GeneratedSerializationConstructorAccessor class generated

javatomcatgarbage-collection

提问by Felix

In order to print the GC logs of a web application,Before the tomcat startup,add the following parameters:

为了打印一个web应用的GC日志,在tomcat启动前,添加以下参数:

-Xms256m 
-Xmx512m 
-XX:PermSize=128M 
-XX:MaxPermSize=512M
-Xloggc:D:/TomcatGc.log

However, the following information is printed on the Terminal continuously.

但是,以下信息会连续打印在终端上。

[Unloading class sun.reflect.GeneratedSerializationConstructorAccessor339]
[Unloading class sun.reflect.GeneratedSerializationConstructorAccessor336]
[Unloading class sun.reflect.GeneratedSerializationConstructorAccessor341]
[Unloading class sun.reflect.GeneratedSerializationConstructorAccessor342]
[Unloading class sun.reflect.GeneratedSerializationConstructorAccessor340]

My questions are:

我的问题是:

  1. Why are these classes generated? I'd like to understand this concept, but can't find any information about it.

  2. How can I prevent the GC unloading them?

  1. 为什么会生成这些类?我想了解这个概念,但找不到任何有关它的信息。

  2. 如何防止 GC 卸载它们?

回答by Mohammad Adil

this is because (may be you are using reflection in your application) heap is running out of space and GCis trying to free some memory by unloading unused objects, that is why you see Unloading class sun.reflect.GeneratedSerializationConstructorAccessor

这是因为(可能是您在应用程序中使用反射)堆空间不足,并GC试图通过卸载未使用的对象来释放一些内存,这就是您看到的原因Unloading class sun.reflect.GeneratedSerializationConstructorAccessor

More info -->http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2006-11/msg00122.html

更多信息-->http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2006-11/msg00122.html

回答by user207421

Your first question has been answered by @pXL, but:

@pXL 已经回答了您的第一个问题,但是:

  1. How can I prevent the GC unloading them?
  1. 如何防止 GC 卸载它们?

You can't. Why on earth would you want to? They're no longer referenced, which implies there are no reachable instances, so they are eligible for garbage collection, so they are being garbage-collected, which implies unloading.

你不能。你到底为什么想要?它们不再被引用,这意味着没有可访问的实例,因此它们有资格进行垃圾收集,因此它们正在被垃圾收集,这意味着卸载。

Preventing that would be both pointless and counter-productive.

防止这种情况既毫无意义又适得其反。

回答by Maxim Kirilov

Different type of accessors

不同类型的存取器

The method accessor and constructor accessors are either native or generated. This means that either we use NativeMethodAccessorImpl or GeneratedMethodAccessor for Methods and NativeConstructorAccessorImpl and GeneratedConstructorAccessor for Constructors. The accessor would be a native or generated and is controlled and decided by two system properties:

方法访问器和构造器访问器要么是本机的,要么是生成的。这意味着我们对方法使用 NativeMethodAccessorImpl 或 GeneratedMethodAccessor,对构造函数使用 NativeConstructorAccessorImpl 和 GeneratedConstructorAccessor。访问器可以是原生的或生成的,并由两个系统属性控制和决定:

  1. sun.reflect.noInflation = false (default value is false)
  2. sun.reflect.inflationThreshold = 15 (default value is 15)
  1. sun.reflect.noInflation = false(默认值为false)
  2. sun.reflect.inflationThreshold = 15(默认值为 15)

When the sun.reflect.noInflation is set to true then the accessor used will always be generated and there is no meaning for the system property sun.reflect.inflationThreshold. When the sun.reflect.noInflation is false and the sun.reflect.inflationThreshold is set to 15 (thats the default behavior if not specified) then it means that for the first 15 accesses to the constructor (or methods), a native generator will be used and thereafter a generated accessor will be supplied (from ReflectionFactory) for use.

当 sun.reflect.noInflation 设置为 true 时,将始终生成所使用的访问器,并且系统属性 sun.reflect.inflationThreshold 没有任何意义。当 sun.reflect.noInflation 为 false 并且 sun.reflect.inflationThreshold 设置为 15 时(如果未指定,这是默认行为)那么这意味着对于构造函数(或方法)的前 15 次访问,本机生成器将被使用,然后将提供一个生成的访问器(来自 ReflectionFactory)以供使用。

The Native accessor uses native calls to access information, whereas the generated accessor is all byte code and hence very fast. On the other hand generated accessor needs time to instantiate and load (basically inflates and hence the system properties controlling it has names including 'inflation' word).

Native 访问器使用本地调用来访问信息,而生成的访问器都是字节码,因此速度非常快。另一方面,生成的访问器需要时间来实例化和加载(基本上是膨胀,因此控制它的系统属性的名称包括“膨胀”词)。

More details can be found at the original blog

更多细节可以在原始博客中找到