Discussion on several methods of Freememory,totalmemory,maxmemory in runtime class

Source: Internet
Author: User
Tags xms

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
  1. Package com.trs.tb.encrypt;
  2. Public class Testmemory {
  3. public static void Main (string[] args) {
  4. System.out.println ("Free:" + runtime.getruntime (). Freememory ()/ 1024x768
  5. / 1024);
  6. System.out.println ("total:" + runtime.getruntime (). TotalMemory ()/ 1024x768
  7. / 1024);
  8. System.out.println ("max:" + runtime.getruntime (). MaxMemory ()/ 1024x768
  9. / 1024);
  10. System.out.println ("=============");
  11. Long T = System.currenttimemillis ();
  12. try {
  13. Thread.Sleep (3000);
  14. } catch (Exception ee) {
  15. Ee.printstacktrace ();
  16. }
  17. String[] AAA = new string[2000000];
  18. System.out.println (Runtime.getruntime (). Freememory ()/ 1024x768/ 1024);
  19. System.out.println (Runtime.getruntime (). TotalMemory ()/ 1024x768/ 1024);
  20. System.out.println (Runtime.getruntime (). MaxMemory ()/ 1024x768/ 1024);
  21. System.out.println ("=============");
  22. try {
  23. Thread.Sleep (3000);
  24. } catch (Exception ee) {
  25. Ee.printstacktrace ();
  26. }
  27. For (int i = 0; i < 2000000; i++) {
  28. Aaa[i] = new String ("AAA");
  29. }
  30. System.out.println (Runtime.getruntime (). Freememory ()/ 1024x768/ 1024);
  31. System.out.println (Runtime.getruntime (). TotalMemory ()/ 1024x768/ 1024);
  32. System.out.println (Runtime.getruntime (). MaxMemory ()/ 1024x768/ 1024);
  33. System.out.println ("=============");
  34. try {
  35. Thread.Sleep (30000);
  36. } catch (Exception ee) {
  37. Ee.printstacktrace ();
  38. }
  39. }
  40. }


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

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.