Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Memory Analyzer (MAT) » Inconsistent display of HashMap-keys
Inconsistent display of HashMap-keys [message #10034] Tue, 14 April 2009 11:43 Go to next message
Stefan Lotties is currently offline Stefan LottiesFriend
Messages: 3
Registered: July 2009
Junior Member
Hi,

I just opened two heap dumps and noticed a HashMap having String's as keys
and wondered why the keys were so big.

The display looks like this:

HashMap
- table java.util.HashMap$Entry[xxx]
-- <class> java.util.HashMap$Entry[]
-- [yyy] java.util.HashMap$Entry @ 0x...
--- <class> java.util.HashMap$Entry
--- key java.lang.String @ 0x... some_string (retained heap: 24)
---- <class> java.lang.String
---- value char[zzz] @ 0x... some_other_string (retained heap: 30,336)

I'm a little bit confused why the key-Member has actually a very low
retained heap size and displays a different string than the char[] inside
the String which has an even bigger retained heap size. This can be seen
on every HashMap-Entry I checked from this map (it contains actually
>100.000 elements ;). Is there anything I'm doing wrong in my very simple
assumption that the key should be the same as the char[] inside it?

Regards,
Stefan
Re: Inconsistent display of HashMap-keys [message #10057 is a reply to message #10034] Tue, 14 April 2009 13:54 Go to previous messageGo to next message
Erwin Margewitsch is currently offline Erwin MargewitschFriend
Messages: 17
Registered: July 2009
Junior Member
Hi Stefan,

your observation results from the way how Strings are implemented/working.

Every String consists of a reference to a char[]. Additionally every
String contains a length and an offset where the actual String inside the
char[] starts. This way many Strings can 'share' one char[]... imagine all
the substring methods which return a new String pointing to the same
char[] with a different offset or a different length.

You can search for the incoming objects for one of the big char[]. I bet
you will find not only one but many Strings ;-)

Regards,
Erwin
Re: Inconsistent display of HashMap-keys [message #10080 is a reply to message #10057] Tue, 14 April 2009 14:52 Go to previous messageGo to next message
Stefan Lotties is currently offline Stefan LottiesFriend
Messages: 3
Registered: July 2009
Junior Member
Hi,

thanks for the information! I checked a couple of char[] an yes, I found a
couple of String-objects that hold the char[] as value-reference. For
example I checked a char[] with a retained heap size of 63,968 bytes and
found two String-objects each with a count of 30.

Somehow this seems to me like a huge design problem of the sun jvm's, but
I think I'm overlooking something very important ;)

Regards,
Stefan
Re: Inconsistent display of HashMap-keys [message #10102 is a reply to message #10080] Tue, 14 April 2009 16:02 Go to previous message
Stefan Lotties is currently offline Stefan LottiesFriend
Messages: 3
Registered: July 2009
Junior Member
Hi again,

mea culpa! I just took a look into the source of String and noticed that
calls like substring() really keep the reference to the original char[]
and the rest is as you told me. I didn't know that fact and well, now I'm
going to be much more careful when using substring() if I know that I'll
keep the sub-string for a longer time.

Thanks for the hint!

Regards,
Stefan
Previous Topic:IOException with (MAT+DTFJ) when reading a phd file
Next Topic:Leak suspect newer end
Goto Forum:
  


Current Time: Sat Nov 29 10:09:59 GMT 2014

Powered by FUDForum. Page generated in 0.01647 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software