简体   繁体   中英

In Java, how do I find out what is my maximum heap size?

I have the following class in Java :

public class MemoryUsage 
{
    public static void main(String[] args)
    {
        Vector v = new Vector();
        while (true)
        {
            byte b[] = new byte[1048576];

            try {
                Thread.sleep(200);
            } catch (InterruptedException ie){}        
            v.add(b);
            Runtime rt = Runtime.getRuntime();
            System.out.println( "free memory: " + rt.freeMemory() );
        }
    }

    public void showCurrentTime() 
    {
        long time = System.currentTimeMillis();
        System.out.println(time);   
    } 
}

And I want to limit how much memory can be used by this program. I tried to set an initial and maximum size (from command-line), like follows :

C:\JMX_code\Memory_test>java -Xms2m -Xmx4068M MemoryUsage
Error occurred during initialization of VM
The size of the object heap + VM data exceeds the maximum representable size

But this doesn't work. How do I figure out what is my own maximum heap size ?

My Java version info. is as follows :

Java(TM) SE Runtime Environment (build 1.7.0_02-b13) Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)

尝试将其更改为:

-Xms2034M -Xmx4068M MemoryUsage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM