Some methods of freememory,totalmemory,maxmemory in runtime class

Source: Internet
Author: User
Tags xms

Some methods of freememory,totalmemory,maxmemory in the runtime class Java Virtual machine THREADJVM recently saw some people on the internet discussing Freememory () in Java.lang.Runtime class , TotalMemory (), maxmemory () Some of the problems of these methods, many people are 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, Many objects have been created, and the return of the Freememory () method is sometimes not reduced, but increases. 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.


After compiling the following source code, in the directory where the class file is located, use JAVA-CP respectively. Untitled1 and JAVA-CP. -xms80m-xmx80m Untitled1 Run to see how the results will help to understand the above elaboration.

Java code
  1. Public class Untitled1 {
  2. Public Untitled1 () {}
  3. public static void Main (string[] args) {
  4. System.out.println (Runtime.getruntime (). Freememory ());
  5. System.out.println (Runtime.getruntime (). TotalMemory ());
  6. System.out.println (Runtime.getruntime (). MaxMemory ());
  7. Long T = System.currenttimemillis ();
  8. try {
  9. Thread.Sleep (30000);
  10. } catch (Exception ee) {
  11. Ee.printstacktrace ();
  12. }
  13. String[] AAA = new string[2000000];
  14. System.out.println (Runtime.getruntime (). Freememory ());
  15. System.out.println (Runtime.getruntime (). TotalMemory ());
  16. System.out.println (Runtime.getruntime (). MaxMemory ());
  17. try {
  18. Thread.Sleep (30000);
  19. } catch (Exception ee) {
  20. Ee.printstacktrace ();
  21. }
  22. For (int i = 0; i < 2000000; i++) {
  23. Aaa[i] = new String ("AAA");
  24. }
  25. System.out.println (Runtime.getruntime (). Freememory ());
  26. System.out.println (Runtime.getruntime (). TotalMemory ());
  27. System.out.println (Runtime.getruntime (). MaxMemory ());
  28. try {
  29. Thread.Sleep (30000);
  30. } catch (Exception ee) {
  31. Ee.printstacktrace ();
  32. }
  33. }
  34. }
[Java]View Plaincopyprint?
  1. Public class Untitled1 {
  2. Public Untitled1 () {}
  3. public static void Main (string[] args) {
  4. System.out.println (Runtime.getruntime (). Freememory ());
  5. System.out.println (Runtime.getruntime (). TotalMemory ());
  6. System.out.println (Runtime.getruntime (). MaxMemory ());
  7. Long T = System.currenttimemillis ();
  8. try {
  9. Thread.Sleep (30000);
  10. } catch (Exception ee) {
  11. Ee.printstacktrace ();
  12. }
  13. String[] AAA = new string[2000000];
  14. System.out.println (Runtime.getruntime (). Freememory ());
  15. System.out.println (Runtime.getruntime (). TotalMemory ());
  16. System.out.println (Runtime.getruntime (). MaxMemory ());
  17. try {
  18. Thread.Sleep (30000);
  19. } catch (Exception ee) {
  20. Ee.printstacktrace ();
  21. }
  22. For (int i = 0; i < 2000000; i++) {
  23. Aaa[i] = new String ("AAA");
  24. }
  25. System.out.println (Runtime.getruntime (). Freememory ());
  26. System.out.println (Runtime.getruntime (). TotalMemory ());
  27. System.out.println (Runtime.getruntime (). MaxMemory ());
  28. try {
  29. Thread.Sleep (30000);
  30. } catch (Exception ee) {
  31. Ee.printstacktrace ();
  32. }
  33. }
  34. }

Some methods of freememory,totalmemory,maxmemory in runtime class

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.