- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我可以从垃圾收集器中看到这种奇怪的行为
public class A {
public static void main(String[] args) {
String foo;
try {
foo = "bar";
int yoo = 5; //1
} catch (Exception e) {
}
int foobar = 3;//2
}
}
如果我去调试并在//1 foo 不是 null 并且它的值为“bar”但在断点处放置断点//2 foo 是 null,这在调试时可能很难理解。我的问题是是否有任何规范说明这是垃圾收集器的合法行为
有了这个小变化,它就不会收集垃圾:
public class A {
public static void main(String[] args) {
String foo;
try {
foo = "bar";
} catch (Exception e) {
throw new RuntimeException(e);
}
int foobar = 3;
}
}
为什么?
最佳答案
在这种情况下,您在设置 foo 变量后不使用它,因此 JVM 完全忽略该变量甚至是合法的,因为它从未使用过并且不会改变程序的结果。
然而,这在 Debug模式下不太可能发生。
在您的情况下,只要 foo 在范围内或者您持有对它的引用(包括 try/catch block 之后的部分),它就不应该被 GC。
编辑
实际上,我得到的行为与您在使用 Java 7.0_03 的 Netbeans 7.1.1 中描述的行为相同...
一个问题可能是因为您没有为 foo
设置默认值,所以您不能在 try/catch block 之后使用它(它不会编译)。
字节码
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String bar
2: astore_1
3: iconst_5
4: istore_2
5: goto 9
8: astore_2
9: iconst_3
10: istore_2
11: return
String foo = null;
作为第一条语句,在这种情况下,调试器会在 try/catch block 之后看到值:public static void main(java.lang.String[]);
Code:
0: aconst_null
1: astore_1
2: ldc #2 // String bar
4: astore_1
5: iconst_5
6: istore_2
7: goto 11
10: astore_2
11: iconst_3
12: istore_2
13: return
我不是字节码专家,但他们看起来和我很相似......
结论
我个人的结论是,要让调试器显示 foo
的值,它必须运行某种类型的 foo.toString()
,这不是作为 foo
的 catch block 之后的有效语句可能尚未初始化。在该部分添加 System.out.println(foo)
是不合法的(不编译)。调试器有点迷失了值是什么,并显示 null
。
为了说服自己这与 GC 无关,您可以尝试以下示例:
public static void main(String[] args){
String foo;
char[] c = null;
try {
foo = "bar";
c = foo.toCharArray();
int yoo = 5; //1
} catch (Exception e) {
}
int foobar = 3;//2
}
在 foobar
行中,您可以看到 c
包含 bar
但 foo 显示为 null
。所以字符串仍然存在,但调试器无法显示它。
更有趣的例子:
public static void main(String[] args){
String foo;
List<String> list = new ArrayList<String>();
try {
foo = "bar";
list.add(foo);
int yoo = 5; //1
} catch (Exception e) {
}
int foobar = 3;//2
}
在 foobar
行,foo
显示为 null
,但 list
包含 "bar"
... 不错。
关于java - 变量在 catch block 后立即被垃圾收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11474743/
引用网址 http://hi.baidu.com/quiteuniverse/blog/item/9f3f043d46ad1e07bba16716.html 以下函数调用方式:&nbs
我什至不确定如何描述我正在尝试做的事情,因为我对 cookie 了解不多,但就这样吧。 是否可以使用PHP从浏览器缓存中收集一个cookie(或cookie文件),将其保存到数据库中,然后清除缓存并重
我正在使用 Room(v. 2.2.1)和协程支持(v. 1.3.2)并进行以下设置 @Entity(tableName = "simple_table") data class SimpleEnti
我正在尝试编写一个基于时间运算符收集/累积值的规则。 rule "Zone6 Overlap" when $i1 : Instance ($e1 : event == " Vel : 20.9
我有一个简单的 BST,定义了节点结构: struct node { int key_value; struct node *left; struct node *right; }; ty
我有这个对象: public class MenuPriceByDay implements Serializable { private BigDecimal avgPrice; p
我正在开发一个应用程序,需要访问给定传感器的“最后 5 秒有值(value)的数据”。我的计划是以某种方式存储这些数据,然后当我请求数据时,它将返回最近 5 秒内获得的所有数据。鉴于以下情况,我不确定
在 Ruby 中,您可以对数组使用 map/collect 方法来修改它: a = [ "a", "b", "c", "d" ] a.collect! {|x| x + "!" } a
我即将开始实时收集大量数字数据(对于那些感兴趣的人,各种股票和 future 的出价/要价/最后或“磁带”)。稍后将检索数据以进行分析和模拟。这一点都不难,但我想高效地做到这一点,这会带来很多问题。我
我提出这个问题是为了寻求有关如何设计系统的实用建议。 像 amazon.com 和 pandora 这样的网站拥有并维护着庞大的数据集来运行他们的核心业务。例如,亚马逊(以及所有其他主要电子商务网站)
假设我们有一个数据数组和另一个带索引的数组。 data = [1, 2, 3, 4, 5, 7] index = [5, 1, 4, 0, 2, 3] 我们想从 index 的 data 元素创建一个
好的,我已经阅读了几个关于它的主题,但现在就开始吧。假设我有一个应用程序,基本上我会时不时地点击一个按钮,几分钟内会发生很多事情,然后它可能会再闲置一个小时,或者可能只是 1 分钟。难道不是在整个结束
我有一个数据框,例如 Seq Chrm start end length score 0 A C1 1 50 49 12 1 B
我正在考虑在 Object[] 数组中收集泛型方法的所有方法参数以进行记录。我知道使用方面可以更好地实现这一点,但是我不允许使用它,并且如果可能的话我正在寻找一种基于纯反射的方法 为了澄清, 假设一个
快速提问: 如果 Socket 对象(及其本地缓存的 InputStream 和 OutputStream 对象)超出范围并被垃圾收集,连接是否在 JVM 中保持打开状态? (即,不会在监听服务器上抛
是否有用于收集 facebook 公共(public)数据作为实时提要的 API。我阅读了关于用于收集数据的公共(public)提要 API,但我现在不能申请,而且它不是免费的,还有 Open str
摘要 :我使用自定义收集器收集给定搜索的所有命中的文档 ID(它使用 ID 填充 BitSet)。根据我的需要,搜索和获取文档 ID 的速度非常快,但是当涉及到从磁盘实际获取文档时,事情变得非常缓慢。
我正在寻找一种方法来从自定义 Gradle 插件收集给定项目的所有依赖约束(通过常规 platform 和/或 enforcedPlatform 和/或“手动”强制执行)。 在 Maven 世界中,您
我有一个 CSV 格式的用户列表,但我需要按广告中的名称从每个用户收集 SamAccount 属性。 CSV 模型 脚本 Get-ADObject -Filter 'ObjectClass -eq "
我得到了一个非常大的列表,其中包含大约 200 个带有文本和图像的项目。 ng-repeat 是一种缓慢渲染的方式。它尝试过这个 solution 。效果很好。但不适合重复收集。 我的网络服务返回此:
我是一名优秀的程序员,十分优秀!