Redian新闻
>
java知道一个reference怎么删掉它指向的内存空间? (转载)
avatar
java知道一个reference怎么删掉它指向的内存空间? (转载)# Java - 爪哇娇娃
s*i
1
avatar
B*g
2
新手表示看不懂,这个不是gc会处理吗?

【在 s*i 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: sci (ence), 信区: JobHunting
: 标 题: java知道一个reference怎么删掉它指向的内存空间?
: 发信站: BBS 未名空间站 (Thu Jun 9 11:51:36 2011, 美东)
: java。
: 维护了一个 HashMap 和 List, A是我自己定义的一个class.
: 每次new一个A之后,先add到 list里面,然后再 加到map里面。
: A a = new A();
: list.add(a);
: map.put(key, a);

avatar
s*i
3
可是我怀疑gc会不会清理掉那个需要被remove的object,因为他还是被referenced了,
reference count != 0, 所以gc应该不会清理它。

【在 B*****g 的大作中提到】
: 新手表示看不懂,这个不是gc会处理吗?
avatar
s*i
4
可是我怀疑gc会不会清理掉那个需要被remove的object,因为他还是被referenced了,
reference count != 0, 所以gc应该不会清理它。

【在 B*****g 的大作中提到】
: 新手表示看不懂,这个不是gc会处理吗?
avatar
g*g
5
A value = map.remove(key)
list.remove(value)

【在 s*i 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: sci (ence), 信区: JobHunting
: 标 题: java知道一个reference怎么删掉它指向的内存空间?
: 发信站: BBS 未名空间站 (Thu Jun 9 11:51:36 2011, 美东)
: java。
: 维护了一个 HashMap 和 List, A是我自己定义的一个class.
: 每次new一个A之后,先add到 list里面,然后再 加到map里面。
: A a = new A();
: list.add(a);
: map.put(key, a);

avatar
m*r
6
我没有看懂, 这个c怎么就可以更容易解决了?

【在 g*****g 的大作中提到】
: A value = map.remove(key)
: list.remove(value)

avatar
g*g
7
我也不明白

【在 m****r 的大作中提到】
: 我没有看懂, 这个c怎么就可以更容易解决了?
avatar
B*g
8
学习

【在 g*****g 的大作中提到】
: A value = map.remove(key)
: list.remove(value)

avatar
z*e
9
为什么reference count!=0?你只有两个地方reference它,一个在map,一个在list,
两边都
remove的话,reference count == 0.

【在 s*i 的大作中提到】
: 可是我怀疑gc会不会清理掉那个需要被remove的object,因为他还是被referenced了,
: reference count != 0, 所以gc应该不会清理它。

avatar
s*i
10
how does this list.remove(value) work in java?
will it do a linear scan in the list ? im looking for a more efficient way.

【在 g*****g 的大作中提到】
: A value = map.remove(key)
: list.remove(value)

avatar
r*l
11
List is just an interface. The implementation depends the classes that
implements the interface.
Are you sure that you want List, not Set? Remove operation in a List is not
that efficient in Java, at least for ArrayList and LinkedList.

【在 s*i 的大作中提到】
: how does this list.remove(value) work in java?
: will it do a linear scan in the list ? im looking for a more efficient way.

avatar
r*y
12
俺没记错的话
A *a;
a = new A;
/* blah blah */
delete a;
a = 0;
然后整个世界就清静了...

【在 m****r 的大作中提到】
: 我没有看懂, 这个c怎么就可以更容易解决了?
avatar
r*y
13
all out-box list implementations in JDK are using sequential search, which
achieve O(N)...
for better performance, you need your own implementation to do indexing,
hashing... whatever

【在 s*i 的大作中提到】
: how does this list.remove(value) work in java?
: will it do a linear scan in the list ? im looking for a more efficient way.

avatar
r*y
14
hmmm...
if the scope of "A a" is big, need to null out it too...

【在 g*****g 的大作中提到】
: A value = map.remove(key)
: list.remove(value)

avatar
g*g
15
得,C++里你把a放到一个list,然后不dereference的话,
就是一个悬挂指针。

【在 r***y 的大作中提到】
: 俺没记错的话
: A *a;
: a = new A;
: /* blah blah */
: delete a;
: a = 0;
: 然后整个世界就清静了...

avatar
g*g
16
If you are looking for efficiency, you can use a TreeSet to back
your list. Or if you don't use list at all, why do you need another data
structure, HashMap.values() doesn't work for you?

【在 s*i 的大作中提到】
: how does this list.remove(value) work in java?
: will it do a linear scan in the list ? im looking for a more efficient way.

avatar
r*y
17
Ooooooooops!
goodbug is right, thanks for correcting me...
my c++ knowledge has been totally gone... :-P

【在 g*****g 的大作中提到】
: 得,C++里你把a放到一个list,然后不dereference的话,
: 就是一个悬挂指针。

avatar
s*e
18
list.remove(a) can be a dangerous game if it is called inside a loop.
you may get the famous ConcurrentModificationException.
using iterator.
avatar
x*i
19
最后 a= null即可,一般下次gc就没了
avatar
z*e
20
removeAll()是我的最爱啊

【在 s******e 的大作中提到】
: list.remove(a) can be a dangerous game if it is called inside a loop.
: you may get the famous ConcurrentModificationException.
: using iterator.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。