Yijie Tao
Yijie Tao

Reputation: 127

Is subMap of NavigableMap going to cause memory leak?

In Java, NavigableMap has a method subMap() which returns a view of the portion of this map whose keys range from fromKey to toKey. In the documentation, it says that the returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.

So my question is if you assign the subMap of a NavigableMap to itself, is it going to cause a potential memory leak? I mean part of the original map is not referenced any more.

NavigableMap<E> map = map.subMap(fromKey, toKey);

Upvotes: 2

Views: 165

Answers (1)

utdemir
utdemir

Reputation: 27226

Yes, it can possibly create a cyclic reference.

But, Java's GC doesn't have a problem with cyclic references. Basically, it scans the whole heap once in a while and removes the ones aren't reachable.

Upvotes: 1

Related Questions