由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - java知道一个reference怎么删掉它指向的内存空间? (转载)
相关主题
JDK Source?is access to int[] faster than List?
哪位大哥总结一下Iterator这些数据集合Is this a Bug or not?
几个Java面试题faint, unreachable statement in java
那个快?help for running CPU intensive program!!
Java Map 存 1 million 记录Sun的JDK 1.5有什么新特点吗?
知道一个key的value, 能不能O(1)从HashMap里拿出对应key和valueJava 初学者问题
JDK HashMap 的 Entry class再请教一个 编译错误
List, LinkedList and VectorInterview的人问我最新java 版本是多少
相关话题的讨论汇总
话题: list话题: java话题: reference话题: remove话题: value
进入Java版参与讨论
1 (共1页)
s*i
发帖数: 388
B*****g
发帖数: 34098
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);

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

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

【在 B*****g 的大作中提到】
: 新手表示看不懂,这个不是gc会处理吗?
g*****g
发帖数: 34805
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);

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

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

g*****g
发帖数: 34805
7
我也不明白

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

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

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

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

s*i
发帖数: 388
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)

相关主题
知道一个key的value, 能不能O(1)从HashMap里拿出对应key和valueis access to int[] faster than List?
JDK HashMap 的 Entry classIs this a Bug or not?
List, LinkedList and Vectorfaint, unreachable statement in java
进入Java版参与讨论
r*****l
发帖数: 2859
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.

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

【在 m****r 的大作中提到】
: 我没有看懂, 这个c怎么就可以更容易解决了?
r***y
发帖数: 4379
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.

r***y
发帖数: 4379
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)

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

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

g*****g
发帖数: 34805
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.

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

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

s******e
发帖数: 493
18
list.remove(a) can be a dangerous game if it is called inside a loop.
you may get the famous ConcurrentModificationException.
using iterator.
x***i
发帖数: 585
19
最后 a= null即可,一般下次gc就没了
z****e
发帖数: 54598
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.

1 (共1页)
进入Java版参与讨论
相关主题
Interview的人问我最新java 版本是多少Java Map 存 1 million 记录
请教大家一些问题知道一个key的value, 能不能O(1)从HashMap里拿出对应key和value
soft reference和weak reference的区别JDK HashMap 的 Entry class
Windows 下 Java console application 的问题List, LinkedList and Vector
JDK Source?is access to int[] faster than List?
哪位大哥总结一下Iterator这些数据集合Is this a Bug or not?
几个Java面试题faint, unreachable statement in java
那个快?help for running CPU intensive program!!
相关话题的讨论汇总
话题: list话题: java话题: reference话题: remove话题: value