How do I allocate more RAM to Java?
A java application I’m using keeps crashing, when I had a problem with a similar application I managed to fix it by allocating more of my RAM to Java. How do I do that? (I recently upgraded to 16 GB RAM, after installing Java.) I’m on an iMac running Mac OSX 10.6.6.
1 Answer 1
java -Xmx1024M -Xms1024M -jar *filename*.jar
Xmx is the max amount you want to allocate (in MB) and the Xms is the initial amount. You can replace the 1024 with the amount you prefer.
@Bec The only way I could think that you could do that is IDE specific. I use NetBeans and I know you can edit NetBean’s config file and tell it to use more RAM. I assume you could do the same with other IDE’s. But as for setting a specific amount using the command line, I believe jzd is correct.
Also, this is only for .JAR applications that can be run headless, without GUI. for applications that cannot, you are screwed. does JAVA have a magnificent solution for this since it cannot be set across the board (which is fairly stupid IMO for above mentioned reason. )?
@MichaelTrouw not correct, you can increase memory for any Java application, regardless whether they run headless or with a GUI. Ryan’s solution is JAVA’s standard way to increase the memory. That said you should bear in mind that a lot of application server and IDE’s come with their own config file to pass the -Xmx argument to the JVM. Check the documentation of your software vendor.
How Much Memory Can I Allocate?
If I have 16 GB of RAM on my machine, how much can I allocate to a java command line program I’m executing? I assume java -Xmx 16g. will crash my system? EDIT:
In light of the comments, I tried java -Xmx16g. , and it did not crash my machine. The program still ran out of memory. I tried java -Xmx32g. , which did crash my machine. From the comments below (which have been really enlightening), I guess I just need to keep playing around with the allocations.
Try it and see. OS’s provide swap space that make the total amount of virtual memory available larger than the amount of physical memory present, as long as not all the memory is needed simultaneously (pages can be «swapped out»).
@RC. — I know it was kind of overkill. But I do huge data processing, and don’t always have access to my school’s servers. EDIT: I was saving up for over a year, and since Apple kept delaying the release date of the 2013 MBP, I just kept saving more. 🙂
@njzk2 Hey, I used to program an Apple II+ with 56KB of RAM! I also used to walk five miles to school, uphill both ways .
2 Answers 2
The size the heap memory of the virtual machine is controlled by the options — Xms and — Xmx .
The first specifies the initial heap size and the second maximum size.
The allocation is done within the JVM itself ,and not on the OS. As more memory is needed, the JVM allocates a block of memory until the Xmx is reached.
If you need more than that, an OutOfMemoryError is thrown.
It is common to use the same value for Xms and Xmx, causing the VM to allocate memory in the operating system only at the beginning of an execution, not depending on the OS specific behavior.
In your case, since you set 32g as your Xmx and your system crashed, it seems you don’t have this memory (swap + memory ram)
If you manually change the virtual memory size of your system, the configuration will work.
Allocate more memory to java application
My boss asks me to develop a simple Java application to sort xls file containing a list of keywords. Everything works fine, but for big xls files my app take very, very long time to do her job. I think that my algorithms aren’t really optimized but I’ve seen that the execution of my jar doesn’t impact at all the performances of my computer (Windows 7 Pro 32 bits of my boss), and in fact it uses really nothing on the CPU and memoty. So, here’s my question, how do I allow my program to take every thing he needs in power and memory to run and sort that xls like a Lamborghini ? Thanks ! EDIT: Here is my code, divides in two block: a main and a frame class:
public class Frame extends JFrame implements ActionListener < /** * */ private static final long serialVersionUID = 1L; JTextField input; JTextField output; JTextField result; JTextArea gomin; JTextArea info; Frame() < JFrame jfrm = new JFrame("App CRI"); jfrm.setLocation(100, 100); jfrm.setLayout(new BorderLayout()); jfrm.setSize(400, 170); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setResizable(false); input = new JTextField(10); output = new JTextField(10); result = new JTextField(10); gomin = new JTextArea(); result.setEditable(false); info = new JTextArea("Merci de renseigner le nom des fichiers sans extension.\nExemple: \"10-09-2014\" pour le fichier \"10-09-2014.xls\"."); info.setEditable(false); // set input fields JPanel inFieldPane = new JPanel(); inFieldPane.setLayout(new GridLayout(2,2)); inFieldPane.add(new JLabel("Input file name:")); inFieldPane.add(input); input.addActionListener(this); inFieldPane.add(new JLabel("Output file name:")); inFieldPane.add(output); output.addActionListener(this); jfrm.add(inFieldPane,BorderLayout.NORTH); // set submit button JPanel submitPane = new JPanel(); submitPane.setLayout(new FlowLayout()); submitPane.add(info); JButton submitButton = new JButton("Let's go !"); submitButton.addActionListener(this); submitPane.add(submitButton); jfrm.add(submitPane,BorderLayout.CENTER); // display results JPanel outFieldPane= new JPanel(); outFieldPane.setSize(350, 50); outFieldPane.add(gomin); gomin.setSize(350, 50); gomin.setEditable(false); jfrm.add(outFieldPane,BorderLayout.SOUTH); jfrm.setVisible(true); >public void actionPerformed(ActionEvent e) < if(e.getActionCommand().equals("Let's go !")) < gomin.setText(Main.doIt(input.getText().trim(), output.getText().trim())); >> >
And here is my main class with all my methods, I haven’t separated these methods in other classes even if I have too, and the quality isn’t pretty bad I know:
public class Main < public static ArrayListloadKeywords(String src) < ArrayListarr = new ArrayList(); try < InputStream inp = new FileInputStream(src); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); Row r; for(int i = 1; i > catch (FileNotFoundException e) < System.out.println("Keywords file undetected !"); e.printStackTrace(); >catch (InvalidFormatException e) < System.out.println("Invalid format. Oups."); e.printStackTrace(); >catch (IOException e) < System.out.println("Fuck ! IOException !"); e.printStackTrace(); >return arr; > public static boolean isValidCri(String inputString, ArrayList arr) < for(int i =0; i < arr.size(); i++) < if(Pattern.compile(Pattern.quote(arr.get(i)), Pattern.CASE_INSENSITIVE).matcher(inputString).find()) < return true; >> return false; > public static void process() < System.out.println("Process en cours . "); ArrayListarr = loadKeywords("C:\\Users\\9108250x\\Desktop\\app_cri\\keywords.xls"); if(arr.isEmpty()) System.out.println("rat�, erreur au chargement des keywords :("); else < try < InputStream inp = new FileInputStream("C:\\Users\\9108250x\\Desktop\\app_cri\\01-09-2014.xls"); Workbook wb = WorkbookFactory.create(inp); Sheet itaSheet = wb.getSheetAt(0); Row r; int endy = itaSheet.getLastRowNum(); for(int i = 1; i > else < itaSheet.shiftRows(i+1, itaSheet.getLastRowNum()+1, -1); i--; endy--; >> FileOutputStream fileOut = new FileOutputStream("C:\\Users\\9108250x\\Desktop\\app_cri\\outPut.xls"); wb.write(fileOut); fileOut.close(); > catch (FileNotFoundException e) < System.out.println("File not found, bad url."); e.printStackTrace(); >catch (InvalidFormatException e) < System.out.println("Invalid file format."); e.printStackTrace(); >catch (IOException e) < System.out.println("Fuck, doesn't work !"); e.printStackTrace(); >System.out.println("et termin� avec succ�s !"); > > public static String doIt(String input, String output) < String result = "succeed !"; ArrayListarr = loadKeywords(".\\keywords.xls"); if(arr.isEmpty()) result = "no keywords"; else < try < InputStream inp = new FileInputStream(".\\"+ input +".xls"); Workbook wb = WorkbookFactory.create(inp); Sheet itaSheet = wb.getSheetAt(0); Row r; int endy = itaSheet.getLastRowNum(); for(int i = 1; i > else < itaSheet.shiftRows(i+1, itaSheet.getLastRowNum()+1, -1); i--; endy--; >> FileOutputStream fileOut = new FileOutputStream(".\\"+ output +".xls"); wb.write(fileOut); fileOut.close(); > catch (FileNotFoundException e) < System.out.println("File not found, bad url."); result = "file not found"; e.printStackTrace(); >catch (InvalidFormatException e) < System.out.println("Invalid file format."); result = "invalid operation"; e.printStackTrace(); >catch (IOException e) < System.out.println("Doesn't work !"); result = "IO exception"; e.printStackTrace(); >catch ( Exception e) < result = "unknow exception"; >System.out.println("et termin� avec succ�s !"); > return result; > /** * @param args */ public static void main(String[] args) < SwingUtilities.invokeLater(new Runnable() < public void run() < new Frame(); >>); // process(); > >
Starting memory allocation for JVM
I’m beginning use the -Xmx option on the java command to allow my processes to use a little more memory (256Mb, though I think I’m currently using less than 128Mb). I’ve also noticed the -Xms option for starting memory, with a default value of 2Mb. What should I set this value to and why?
I admit I’m too lazy to look it up but my general rule of thumb for questions like this is: If I don’t know what the option is good for and nothing’s broken, I leave it as is, trusting to the good sense of whoever set the default.
4 Answers 4
The -Xmx argument defines the max memory size that the heap can reach for the JVM. You must know your program well and see how it performs under load and set this parameter accordingly. A low value can cause an OutOfMemoryException or very poor performance if your program’s heap memory is reaching the maximum heap size. If your program is running on a dedicated server you can set this parameter higher because it wont affect other programs.
The -Xms argument sets the initial and minimum heap memory size for the JVM. This means that when you start your program the JVM will allocate this amount of memory instantly. This is useful if your program will consume a large amount of heap memory right from the start. This avoids the JVM needing to regularly increase the heap size and so you can gain some performance there. If you don’t know if this parameter is going to help you, don’t use it.
It is good practice with server-side Java applications like Resin to set the minimum -Xms and maximum -Xmx heap sizes to the same value. You can set to 256 or 512Mb.
My practice is that when I find I have to raise mx for some reason, I make ms match it since it IS hitting that limit (or you wouldn’t have had to raise mx). I’m not sure if this matters, but why not have it allocate it all up front if you know you are going to need it? If you don’t need the high mx for every run though you might leave ms low.
@Bill yeap, nowadays RAM is a something you can just throw in into a machine. This was more for a time when the app needed to play nice with the rest. If you have the RAM you can give it to java upfront.
Well, my thought was that under the assumption that you need the ram and have to use your -mx variable anyway, then it follows that you have the ram to use -ms and will eventually need it so where is the negative to setting it up front?
Heap size should be ‘right’ for your application finding right though isn’t easy. If it’s too small you run out of memory if to big you waste memory and will run the risk of having long GC pauses. When things go wrong (and they always go wrong) the more heap you have the more heap you have to debug and the longer any leaks will take to manifest themselves.
Ideally I would set -Xms to be the size I think my application needs to run and -Xmx to a value larger than this (but not too large). Always switch on verbose garbage collection and graph the heap in use to check the values you have set.
When I graph the heap I look at the number of collections occurring and the type. It’s important not to have too many. However it’s also important not to have a heap so huge that there are few, because when they happen (and full gc’s are inevitable) they will hurt. Ideally a regular set of well spaced gcs with very low pause times is being sought.
That said for 32bit Windows -Xms should be the same as -Xmx this is because Java (leaving aside jrockit) needs contiguous memory and the Windows’ memory map is fragmented it therefore pays to get and lock down the heap as early as possible.