Recently on the Internet to see some people discussed in the Java.lang.Runtime Class Freememory (), TotalMemory (), maxmemory () Some of the topics of these methods, many people feel very confused, why, When the Java program just started up freememory () This method returns only one or two megabytes, and as the Java program runs forward, creating a lot of objects, freememory () The return of this method is sometimes not only not reduced, but will increase. These people should have some misunderstanding about the meaning of the Freememory () method, which they thought would return the remaining available memory of the manipulation system, which is simply not the case. These three methods reflect the memory of the Java process, which is not related to the memory of the operating system. This is explained in conjunction with TotalMemory (), MaxMemory ().
MaxMemory () This method returns the maximum amount of memory that the Java Virtual machine (this process) can construct from the manipulation system, in bytes, if the Java program is running without adding the-XMX parameter, then it is 64 trillion, which means maxmemory () The return is approximately 64*1024*1024 bytes, which is the maximum memory that the Java Virtual machine can dig from the manipulation system by default. If the-XMX parameter is added, the value following this parameter will prevail, such as JAVA-CP classpath-xmx512m ClassName, then the maximum memory is 512*1024*0124 bytes.
TotalMemory () This method returns the amount of memory that the Java Virtual machine has now dug up from the manipulation system, that is, all the memory occupied by the Java Virtual machine process at that time. If you do not add-XMS parameters when running Java, then, in the Java program running process, memory is always slowly from the control system dug, basically is how much digging, straight to maxmemory () so far, so totalmemory () is slowly increasing. If the-XMS parameter is used, the program will start with unconditional digging from the control system-the amount of memory defined by XMS, and then digging when the memory is contempoly.
Freememory () is what, just talked about if you run Java without adding-XMS parameters, then, in the Java program running process, memory is always slowly from the operating system dug, basically is how much digging how much, But Java Virtual machine 100% in the case of a little bit more digging, these dug and no use of memory, is actually freememory (), so the value of freememory () is generally very small, However, if you are running a Java program using-XMS, this time because the program at the start of the process will be unconditionally dug from the control system-XMS the number of memory defined later, this time, the memory dug up may be mostly useless, so this time freememory () may be a bit large.
Several methods of Freememory,totalmemory,maxmemory in runtime class