0

我正在尝试将一大堆对象添加到 db4o 数据库中。似乎您必须为每个对象创建一个新对象,因为如果我只是更新旧对象中的值并再次存储它,它只会覆盖数据库中的记录,而我只会得到一条记录。所以这就是我所拥有的:

for (String o : d.students) {
    Student stu1 = new Student(o);
    db.store(stu1);
    stu = null;
}

我的问题显然是我没有对刚刚存储的对象做任何事情,只是重新分配指针。我知道 Java 的垃圾收集应该处理所有事情,但我也知道 Java 知道泄漏,所以如果有更安全的方法来解决这个问题,我宁愿这样做。

提前非常感谢!

4

1 回答 1

2

I think your assertion that "Java's known for being leaky" is misguided at best.

Simply assigning a variable in the scope of the loop is sufficient to trigger its collection outside the loop, assuming that the db component doesn't hold that reference. Setting the variable to null in the above is an unnecessary optimisation.

于 2012-08-03T15:53:18.247 回答