我想知道如何垃圾收集工作时,你有一个类与反射用于获取一些字段值。 JVM如何意识到这些字段引用的值是可访问的,因此当前不使用正式语言语法访问垃圾回收时不适合进行垃圾回收?垃圾收集和反射
一个小片段显示问题(虽然反射一直过分强调这里):
/**
*
*/
import java.lang.reflect.Field;
public class B {
protected B previous = null, next = null;
/**
*
*/
public B(B from) {
this.previous = from;
}
public void transition(B to) {
this.next = to;
}
public B next() {
try {
Field f = getClass().getField("next");
f.setAccessible(true);
try {
return (B)f.get(this);
} finally {
f.setAccessible(false);
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public B previous() {
try {
Field f = getClass().getField("previous");
f.setAccessible(true);
try {
return (B)f.get(this);
} finally {
f.setAccessible(false);
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
干杯,
克里斯
有趣的问题。它是否受某种嵌入式Java应用程序的启发? –
你能否给我们一个代码片段来澄清你的意思:“一个带反射的类用于获取一些字段值?” –
实际上,我正在进行网络框架的测试,并且有些字段是通过反射来访问的。想知道反射如何影响内存使用,也许JVM保留对象是不确定的。 –