Recently seen on the Internet some people discussed the Java.lang.Runtime class in the Freememory (), TotalMemory (), maxmemory () Some of the problems of these methods, many people feel very puzzled, 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 think is returning the remaining available memory of the operating system, which is not the case at all. These three methods reflect the memory situation of the Java process, which is not related to the operating system's memory at all. 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 operating 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 operating 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 operating 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 operating system dug, basically how much digging how much, straight to maxmemory () so far, so totalmemory () is slowly increasing. If the-XMS parameter is used, the program will start with an unconditional dig from the operating system-the amount of memory defined by XMS, and then dig when the memory is about the same.
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 to dig, 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 Java programs using-XMS, this time because the program at boot time will be unconditionally dug from the operating system-XMS the amount of memory defined later, this time, the memory dug up may be mostly useless, so this time freememory () may be a bit large.
I wrote an example and should have a deeper understanding of the test results:
[Java]View Plaincopy
- Package com.trs.tb.encrypt;
- Public class Testmemory {
- public static void Main (string[] args) {
- System.out.println ("Free:" + runtime.getruntime (). Freememory ()/ 1024x768
- / 1024);
- System.out.println ("total:" + runtime.getruntime (). TotalMemory ()/ 1024x768
- / 1024);
- System.out.println ("max:" + runtime.getruntime (). MaxMemory ()/ 1024x768
- / 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 ()/ 1024x768/ 1024);
- System.out.println (Runtime.getruntime (). TotalMemory ()/ 1024x768/ 1024);
- System.out.println (Runtime.getruntime (). MaxMemory ()/ 1024x768/ 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 ()/ 1024x768/ 1024);
- System.out.println (Runtime.getruntime (). TotalMemory ()/ 1024x768/ 1024);
- System.out.println (Runtime.getruntime (). MaxMemory ()/ 1024x768/ 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
=============
Discussion on several methods of Freememory,totalmemory,maxmemory in runtime class