📌  相关文章
📜  设置最小和最大堆大小的Java程序

📅  最后修改于: 2022-05-13 01:54:29.944000             🧑  作者: Mango

设置最小和最大堆大小的Java程序

堆区域是 JVM 中存在的各种内存区域之一。每个 JVM 都有一个堆区域可用。 Heap 区域将在JVM启动时创建。对象和相应的实例变量将存储在堆区中。 Java中的每个数组都是对象,因此数组也将存储在堆区中。堆区域可以被多线程访问,因此堆内存中的数据存储不是线程安全的。堆区域不必是连续的。

显示堆内存统计信息的程序:

Java应用程序可以使用运行时对象与 VM 进行通信。运行时类存在于Java.lang 包中,它是一个单例类。我们可以创建一个运行时对象,如下所示:

一旦我们获得了运行时对象,我们就可以在该对象上调用以下方法:



  • maxMemory() :它返回分配给堆的最大内存字节数。
  • totalMemory() :它返回分配给堆的总内存的字节数。
  • freeMemory() :它返回堆中可用内存的字节数
Java
// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;
 
// driver class
class heapMemory {
 
    // main method
    public static void main(String[] args)
    {
        // creating runtime time object
        Runtime r = Runtime.getRuntime();
 
        // displaying max memory of heap in bytes
        System.out.println("Max memory"
                           + " " + r.maxMemory());
 
        // displaying initial memory in bytes
        System.out.println("Initial memory"
                           + " " + r.totalMemory());
 
        // displaying free memory in bytes
        System.out.println("Free memory"
                           + " " + r.freeMemory());
 
        // displaying consume memory in bytes
        System.out.println(
            "Consume memory"
            + " " + (r.totalMemory() - r.freeMemory()));
    }
}


Java
// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;
 
// driver class
class heapMemory {
 
    // main method
    public static void main(String[] args)
    {
        double mb = 1000000;
 
        // creating runtime time object
        Runtime r = Runtime.getRuntime();
 
        // displaying max memory of heap in Mb
        System.out.println("Max memory"
                           + " " + r.maxMemory() / mb);
 
        // displaying initial memory in Mb
        System.out.println("Initial memory"
                           + " " + r.totalMemory() / mb);
 
        // displaying free memory in Mb
        System.out.println("Free memory"
                           + " " + r.freeMemory() / mb);
 
        // displaying consume memory in Mb
        System.out.println(
            "Consume memory"
            + " "
            + (r.totalMemory() - r.freeMemory()) / mb);
    }
}


Java
// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;
 
// driver class
class heapMemory {
 
    // main method
    public static void main(String[] args)
    {
        double mb = 1000000;
 
        // creating runtime time object
        Runtime r = Runtime.getRuntime();
 
        // displaying max memory of heap in Mb
        System.out.println("Max memory"
                           + " " + r.maxMemory() / mb);
 
        // displaying initial memory in Mb
        System.out.println("Initial memory"
                           + " " + r.totalMemory() / mb);
 
        // displaying free memory in Mb
        System.out.println("Free memory"
                           + " " + r.freeMemory() / mb);
 
        // displaying consume memory in Mb
        System.out.println(
            "Consume memory"
            + " "
            + (r.totalMemory() - r.freeMemory()) / mb);
    }
}


输出
Max memory 134217728
Initial memory 134217728
Free memory 132286176
Consume memory 1931552

堆内存是有限的内存,但根据我们的要求,我们可以设置最大和最小堆大小,即我们可以根据我们的要求增加或减少堆大小。我们可以通过在运行时使用以下Java命令来做到这一点。

1. -Xmx设置最大堆大小(最大内存)

此命令将最大堆大小设置为 512Mb。

Java

// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;
 
// driver class
class heapMemory {
 
    // main method
    public static void main(String[] args)
    {
        double mb = 1000000;
 
        // creating runtime time object
        Runtime r = Runtime.getRuntime();
 
        // displaying max memory of heap in Mb
        System.out.println("Max memory"
                           + " " + r.maxMemory() / mb);
 
        // displaying initial memory in Mb
        System.out.println("Initial memory"
                           + " " + r.totalMemory() / mb);
 
        // displaying free memory in Mb
        System.out.println("Free memory"
                           + " " + r.freeMemory() / mb);
 
        // displaying consume memory in Mb
        System.out.println(
            "Consume memory"
            + " "
            + (r.totalMemory() - r.freeMemory()) / mb);
    }
}
输出
Max memory 134.217728
Initial memory 134.217728
Free memory 132.285184
Consume memory 1.932544

2. -Xms :我们可以使用此命令来设置最小或初始堆大小。

此命令将最小大小设置为 64Mb,即 totalMemory()。

Java

// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;
 
// driver class
class heapMemory {
 
    // main method
    public static void main(String[] args)
    {
        double mb = 1000000;
 
        // creating runtime time object
        Runtime r = Runtime.getRuntime();
 
        // displaying max memory of heap in Mb
        System.out.println("Max memory"
                           + " " + r.maxMemory() / mb);
 
        // displaying initial memory in Mb
        System.out.println("Initial memory"
                           + " " + r.totalMemory() / mb);
 
        // displaying free memory in Mb
        System.out.println("Free memory"
                           + " " + r.freeMemory() / mb);
 
        // displaying consume memory in Mb
        System.out.println(
            "Consume memory"
            + " "
            + (r.totalMemory() - r.freeMemory()) / mb);
    }
}
输出
Max memory 134.217728
Initial memory 134.217728
Free memory 132.285192
Consume memory 1.932536