1

I have followed some of the posts in increase JVM heap size Stack Overflow to increase the JVM heap size. I set environment variables as suggested above link.

I executed below program, to check, what is the max heap size.

public class GetHeapSize {
     public static void main(String[]args){

            //Get the jvm heap size.
            long heapSize = Runtime.getRuntime().totalMemory();

            //Print the jvm heap size.
            System.out.println("Heap Size = " + heapSize);
        }

}

I got output before setting and after setting is same as 16252928

Please can anyone suggest how can I increase heap size?

Nicolas Henneaux
  • 11,507
  • 11
  • 57
  • 82
developer
  • 9,116
  • 29
  • 91
  • 150
  • what happens if you try it the plain way, something like java -Xmx1g $name of your main class here$ – Scorpion Nov 07 '12 at 05:34
  • I want to set heap size permanently, as iam going to do fingerprint detection, its getting out of memory when the file size is more than 100MB – developer Nov 07 '12 at 05:36
  • *"I want to set heap size permanently,"* Yes, but it is only for that one app. & you can make the advice of @Scorpion work once for that one app., it will be possible to set up a way to start it that ensures it **always** has that much memory. – Andrew Thompson Nov 07 '12 at 06:09
  • It would be helpful to see what command line options are used, actually. – TheBlastOne Nov 07 '12 at 09:56

2 Answers2

0

The totalMemory() reports the current size of the heap. If you want the maximum size that the heap will grow to, call maxMemory() instead. The javadoc says:

"Returns the maximum amount of memory that the Java virtual machine will attempt to use. If there is no inherent limit then the value Long.MAX_VALUE will be returned."

and then says that the amount is measured in bytes.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • after your suggestion, i get it as 259522560 which is equals to 247.5MB, but i setted in environmental variable as 512m – developer Nov 07 '12 at 05:47
  • I cannot explain that. I am simply repeating what the javadoc says. I suggest that you check that -Xmx and -Xms settings you think you are using are actually getting passed to the JVM when it is launched. – Stephen C Nov 07 '12 at 09:52
0

in the common case the Heap size grows dynamically, so that you can use -Xmx option to limit the high size of the heap and -Xms option to set the start size of the heap and you can make them the same on the start, so that the JVM will not spend resources for dynamic memory operations. Also may be it would be interesting for you to use more informative JMX way to get heap size through the MemoryMXBean bean.

MemoryMXBean MEMORYMXBEAN = ManagementFactory.getMemoryMXBean();
Igor Maznitsa
  • 833
  • 7
  • 12