I recently saw some people discussing Java on the Internet. lang. many people are wondering why freememory (), totalmemory (), and maxmemory () in the runtime class, when the Java Program is started, the freememory () method returns only one or two megabytes, and as the Java program runs forward, A lot of objects have been created. The return value of the freememory () method is sometimes not reduced, but increased. These people should have some misunderstandings about the meaning of the
method freemory (). They think that the method returns the remaining available memory of the operating system, which is actually not the case at all. These three methods reflect the memory condition of the Java Process, which has nothing to do with the memory of the operating system. The following is a combination of totalmemory () and maxmemory.
maxmemory () returns the maximum memory size that the Java Virtual Machine (this process) can dig from the operating system, in bytes, if the-xmx parameter is not added when the Java program is running, it is 64 MB, that is, 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.
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 from the operating system at startup-
The memory size defined after XMS, and then dig again when the memory usage is similar.
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, it is used to dig much, but when Java Virtual Machine 100% is used, it will dig a little more, the memory is actually freememory (), so the value of freememory () is usually very small, however, if you use-XMS when running a Java program
At startup, the system will dig out the memory size defined after XMS from the operating system unconditionally. At this time, most of the acquired memory may be useless. Therefore, freememory () it may be a little big.
I wrote an example myself, so I should have a deeper understanding of the test results:
Package COM. TRS. TB. encrypt; public class testmemory {public static void main (string [] ARGs) {system. out. println ("free:" + runtime. getruntime (). freememory ()/1024/1024); system. out. println ("Total:" + runtime. getruntime (). totalmemory ()/1024/1024); system. out. println ("MAX:" + runtime. getruntime (). maxmemory ()/1024/1024); system. out. println ("=============="); long T = system. currenttimemillis (); try {thread. sleep (3000);} catch (exception ee) {ee. printstacktrace ();} string [] AAA = new string [2000000]; system. out. println (runtime. getruntime (). freememory ()/1024/1024); system. out. println (runtime. getruntime (). totalmemory ()/1024/1024); system. out. println (runtime. getruntime (). maxmemory ()/1024/1024); system. out. println ("=============="); try {thread. sleep (3000);} catch (exception ee) {ee. printstacktrace () ;}for (INT I = 0; I <2000000; I ++) {AAA [I] = new string ("AAA") ;}system. out. println (runtime. getruntime (). freememory ()/1024/1024); system. out. println (runtime. getruntime (). totalmemory ()/1024/1024); system. out. println (runtime. getruntime (). maxmemory ()/1024/1024); system. out. println ("=============="); try {thread. sleep (30000);} catch (exception ee) {ee. printstacktrace ();}}}
Execution time:
Free: 4
Total: 4
MAX: 63
==================
4
12
63
==================
9
63
63
==================