33

I am trying to build a lexicon trie of almost 110000 words in java in netbeans. My code is running fine but it gives an Exception as follows:

   Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:3209)
at java.lang.String.<init>(String.java:215)
at java.nio.HeapCharBuffer.toString(HeapCharBuffer.java:542)
at java.nio.CharBuffer.toString(CharBuffer.java:1157)
at java.util.regex.Matcher.toMatchResult(Matcher.java:232)
at java.util.Scanner.match(Scanner.java:1270)
at java.util.Scanner.nextLine(Scanner.java:1517)
at lexiconbuild.model.Lexicon.<init>(Lexicon.java:29)
at lexiconbuild.model.LexiconBuild.main(LexiconBuild.java:17)
Java Result: 1

I was wondering if someone could help me with increasing the java heap space in netbeans.

Noopur Phalak
  • 878
  • 1
  • 13
  • 22

3 Answers3

53

You can set it in NetBeans in the project properties -> Run -> VM options

  1. Right click on your project "Properties"
  2. Select "Run" category.
  3. Enter your arguments(-Xmx512m) in the "VM Options" text box.

Example: Putting -Xmx512m in the "VM Options" text box gives 512Mb maximum heap size to your Java program.

1218985
  • 7,531
  • 2
  • 25
  • 31
  • 1
    NetBeans changed sometime in the last 4 years, and "Build and Run" doesn't have anywhere intuitive to put commandline arguments now. Only some predefined targets. – i336_ Jul 12 '17 at 09:00
39

if you want to change it for netbeans it self you can change it from this file:

netbeans.conf 

you will find it in netbeans folder under /etc

and there is two parameters for the heap

Xms - the initial size of the heap.

Xmx - max java heap size

it using the default, you just have to add it there and try to add them both.

and also don't forget to add those to netbeans_default_options when you change the XMX

-J-XX:+UseConcMarkSweepGC -J-XX:+CMSClassUnloadingEnabled -J-XX:+CMSPermGenSweepingEnabled

so netbeans_default_options will be something like that:

netbeans_default_options="-J-client -J-Xss2m -J-Xms512m -J-Xmx1024m -J-XX:PermSize=256m -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.zip.disableMemoryMapping=true -J-XX:+UseConcMarkSweepGC -J-XX:+CMSClassUnloadingEnabled -J-XX:+CMSPermGenSweepingEnabled"
Majed
  • 917
  • 6
  • 18
  • Majed, your solution helped me at some point but now that I have an even bigger file when I change the values of Xms and Xmx to 1024 and 2048 I get an error of 'Could not reserve enough space for 2097152kb object heap' any idea how to solve this? – Mahsa Mar 14 '18 at 23:30
3

In the run configuration you should set Java VM option -Xmx512m or other that limits the maximum size of the Java VM to 512M. You can always experiment with other values. You are only limited with physical memory used by the OS.