Re: Memory utilization

Peter Booth <[email protected]> Wed, 30 Jan 2008 11:40:40 -0500
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <[email protected]>
Avinash,

A very crude experiment (say +/- 20%):

I ran your test program on my MacBook Pro after changing the upper  
bound to Integer.MAX_VALUE / 512;

It took 7.5 secs to complete and had a heap size of 572MB just prior  
to exiting.

7.500: [GC 7.500: [DefNew: 72576K->8064K(72576K), 0.4641337 secs]  
584898K->572609K(1040512K), 0.4642092 secs]

1. Changing the code to avoid the unnecessary Integer creation reduced  
execution time to 4.34 sec (42%) and heap size to 313 MB (45%)

        int COUNT = Integer.MAX_VALUE / 512;
           Map<String, String> map = new HashMap<String, String>();
           for( int i = 0; i < COUNT; ++i )
           {
                String s = Integer.toString(i);
                map.put(s,s);
           }

4.340: [GC 4.341: [DefNew: 72576K->8063K(72576K), 0.5675008 secs]  
313491K->313491K(1040512K), 0.5675750 secs]

2. Changing the code to create the HashMap presized, reduced execution  
time to 4.3 sec(5%) and increased heap size to 319 MB(2%)

    Map<String, String> map = new HashMap<String, String>(COUNT);

4.089: [GC 4.089: [DefNew: 72576K->8064K(72576K), 0.5929834 secs]  
318993K->318993K(1040512K), 0.5930458 secs]

There is always more you can do, so long as you know more about your  
data's characteristics than the programmer of the general purpose  
collection code you are using.

Peter

> I was trying to profile the following piece of code using NetBeans:
>
> public static void main(String[] args)
> {
>    Map<String, String> map = new HashMap<String, String>();
>    for( int i = 0; i < Integer.MAX_VALUE; ++i )
>    {
>        map.put(Integer.valueOf(i).toString(),  
> Integer.valueOf(i).toString());
>    }
> }
>
>
> I set my max and min heap size to be 1GB. I find that the program  
> dies after having created 4M entries (at which point the heap used  
> is at 1GB) although the memory used by the objects as indicated by  
> the Netbeans memory profiler is the following:
>
> String -- 48MB
> char[] -- 48 MB
> HeapCharBuffer -- 45MB
> Integer -- 19MB
> HashMap$Entry -- 19MB
>
>
> This totals to less than 200 MB and the rest of the objects are at  
> 0% of the total used memory. How come the heap usage is at 1GB at  
> this point of time? I am extremely confused. Could someone please  
> explain what might be going on?
>
> Thanks
> A
>
> _________________________________________________________________
> Need to know the score, the latest news, or you need your HotmailĀ®- 
> get your "fix".
> http://www.msnmobilefix.com/Default.aspx
> ===================================
> This list is hosted by DevelopMentorĀ®  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com