The hashmap structure and memory in Java

Source: Internet
Author: User
Tags xms
From: http://hi.baidu.com/calls911/blog/item/c0ed39da95379f3f33fa1c98.html

1. Memory occupied by the hashmap Structure
Use the following methods to test the memory size occupied by the hashmap structure itself when storing 1 million key-value pairs:

Hashmap hm1 = new hashmap <integer, integer>;
For (INT I = 0; I <1000000; ++ I ){
Hm1.put (I, I );
}

In this case, one million dual intors are stored, and two intors are 8 bytes. Therefore, the int itself occupies 1 million * 8 bytes, that is, 8 m.

Through Java. lang. the freememory () method in the runtime class is used for testing. Before the freememory is output, a system is called. GC (), it is concluded that the hashmap of a million key-Value Pair occupies 64 MB of space.

2. Several methods such as freememory () of the runtime class (reprinted)

The freememory (), totalmemory (), and maxmemory () methods reflect the memory of the Java Process and have nothing to do with the memory of the operating system.

A. the maxmemory () method returns the maximum memory that the Java Virtual Machine (this process) can dig from the operating system, in bytes. If you are running a Java program, if the-xmx parameter is not added, it is 64 mb. That is to say, maxmemory () returns about 64*1024*1024 bytes, this is the maximum memory that the Java Virtual Machine can dig from the operating system by default. If the-xmx parameter is added, the value following this parameter prevails. For example, Java-CP classpath-xmx512m classname, the maximum memory size is 512*1024*0124 bytes.
B. The totalmemory () method returns the memory size that the Java Virtual Machine has dug from the operating system, that is, all the memory occupied by the Java Virtual Machine process at that time. If the-XMS parameter is not added when running Java, the memory is always slowly dug from the operating system during the Java program running process, basically how much is used to dig, until maxmemory (), totalmemory () increases slowly. If the-XMS parameter is used, the program will unconditionally dig the memory size defined after-XMS from the operating system at startup, and then when the memory usage is similar, then dig.
C. what is freememory ()? If the-XMS parameter is not added while running Java, the process of running Java is as follows, the memory is always dug slowly from the operating system, basically the number of resources to be dug, but the Java Virtual Machine will dig a little more in 100% cases, the memory is actually freememory (), so the value of freememory () is usually very small, however, if you use-XMS when running a Java program, this is because the program will unconditionally dig out the number of memory defined after the XMS from the operating system at startup, at this time, most of the dug memory may be useless, so freememory () may be a little large at this time.

Sample Code:

After compiling the following source code, use Java-CP in the directory where the class file is located. untitled1 and Java-CP. -Run xms80m-xmx80m untitled1 to see how the result is and help you understand the above description.
Package test;

Public class teststr {
Public static void main (string [] ARGs ){
System. Out. println (runtime. getruntime (). freememory ());
System. Out. println (runtime. getruntime (). totalmemory ());
System. Out. println (runtime. getruntime (). maxmemory ());
Long T = system. currenttimemillis ();
Try {
Thread. Sleep (30000 );
} Catch (exception ee ){
Ee. printstacktrace ();
}
String [] AAA = new string [2000000];
System. Out. println (runtime. getruntime (). freememory ());
System. Out. println (runtime. getruntime (). totalmemory ());
System. Out. println (runtime. getruntime (). maxmemory ());
Try {
Thread. Sleep (30000 );
} Catch (exception ee ){
Ee. printstacktrace ();
}
For (INT I = 0; I <2000000; I ++ ){
AAA [I] = new string ("AAA ");
}
System. Out. println (runtime. getruntime (). freememory ());
System. Out. println (runtime. getruntime (). totalmemory ());
System. Out. println (runtime. getruntime (). maxmemory ());
Try {
Thread. Sleep (30000 );
} Catch (exception ee ){
Ee. printstacktrace ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.