从Jvm在64位操作系统中为Heap占用内存的地方?

时间:2012-08-14 05:41:43

标签: java memory memory-management heap

使用命令-Xms500m -Xmx139000m

获得104 Gp堆内存

我们的核心i5处理器64位Windows 7 Os。 500 Gp硬盘。仅限4GB RAM

  1. 我只是想知道Jvm从哪里获取104 GB(堆)的内存?

  2. 在输出中没有显示内存使用情况?

             `public class CPUusage {
         public static void main(String[] args)throws Exception
         {
    int mb = 1024*1024;
    int GB = 1024*1024*1024;
    /* Total number of processors or cores available to the JVM */    
    System.out.println("Available processors (cores): " +          Runtime.getRuntime().availableProcessors());
    /* Total amount of free memory available to the JVM */ 
    System.out.println("Free memory (MB): " +          Runtime.getRuntime().freeMemory()/mb);
    /* This will return Long.MAX_VALUE if there is no preset limit */  
    long maxMemory = Runtime.getRuntime().maxMemory()/GB;
    /* Maximum amount of memory the JVM will attempt to use */    
    System.out.println("Maximum memory (GB): " +          maxMemory);
    /* Total memory currently in use by the JVM */  
    System.out.println("Total memory (MB): " +          Runtime.getRuntime().totalMemory()/mb);
    /* Get a list of all filesystem roots on this system */
    File log=new File("D:\\log.txt");
    log.createNewFile();
    FileWriter fstream = new FileWriter(log);
    BufferedWriter out = new BufferedWriter(fstream);
      out.write("--------------------------------------------"+"\n\n");
    
    out.write("Available processors (cores): " +          Runtime.getRuntime().availableProcessors());
    out.newLine();
    out.write("Free memory (MB): " +          Runtime.getRuntime().freeMemory()/mb);
    out.newLine();
    out.write("Maximum memory (MB): " +          (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
    out.newLine();
    out.write("Total memory (MB): " +          Runtime.getRuntime().totalMemory()/mb); 
    out.newLine();
    
    File[] roots = log.listRoots();
    /* For each filesystem root, print some info */ 
    for (File root : roots) {
        System.out.println("-------------------------------------------");
        System.out.println("File system root: " + root.getAbsolutePath());
        System.out.println("Total space (GB): " + root.getTotalSpace()/GB);
        System.out.println("Free space (GB): " + root.getFreeSpace()/GB);
        System.out.println("Usable space (GB): " + root.getUsableSpace()/GB);
       out.write("-------------------------------------------");
        out.newLine();
        out.write("File system root: " + root.getAbsolutePath());
        out.newLine();
        out.write("Total space (GB): " + root.getTotalSpace()/GB);
        out.newLine();
        out.write("Free space (GB): " + root.getFreeSpace()/GB);
        out.newLine();
        out.write("Usable space (GB): " + root.getUsableSpace()/GB);
        out.newLine();
    
    }
      out.write("-------------------------------------------");
      out.newLine();
      out.close();
        }
        } 
    
               `
    

    输出

             `Available processors (cores): 4
              Free memory (MB): 476
              Maximum memory (GB): 104
              Total memory (MB): 479
              -------------------------------------------
              File system root: C:\
              Total space (GB): 97
              Free space (GB): 70
              Usable space (GB): 70
              -------------------------------------------
              File system root: D:\
              Total space (GB): 368
              Free space (GB): 366
              Usable space (GB): 366
    
              `
    

2 个答案:

答案 0 :(得分:3)

选项“-Xms500m -Xmx139000m”表示“分配初始堆大小为500Mb,并让它增长到最大139GB ......如果需要”。

您从程序中看到的输出与此完全一致。在程序运行时,堆没有达到139Gb。它可能从未达到该水平。它甚至可能能够达到该级别......取决于操作系统能够在何时/何时请求它们的情况下为JVM提供的资源。

如果你真的想强制JVM使用139Gb堆,你应该尝试将139Gb设置为初始堆大小;例如“-Xms139000m -Xmx139000m”。但这可能不是一个好主意,特别是如果你没有那么多的物理RAM。

答案 1 :(得分:0)

虚拟内存意味着RAM在使用时分配,而不是在保留地址空间时分配。如果您实际使用的是104GB,则操作系统将使用swap(磁盘上的文件或分区)来保持比您实际拥有的RAM更多的错觉。