I am currently working my way through a project, and came across an issue. The program I am writing requires big numbers, bigger than int can hold. So, I used Math.BigInteger. After adding many BigIntegers together, I got a OutofMemory error. I am assuming that means the data type is not large enough. Is there a larger data type, and if not, how can I resolve this issue?
-
4No, that's not what the exception means. As its name (and its javadoc) indicates, it means the JVM is running out of memory. – JB Nizet Jan 01 '17 at 16:09
-
Ok, so is it possible to allocate the JVM more memory? Or is it using all the system memory? – OctopusProteins Jan 01 '17 at 16:11
-
1Google for "is it possible to allocate the JVM more memory?", and you'll have your answer. Google is your friend. – JB Nizet Jan 01 '17 at 16:14
-
2You are probably doing something horribly wrong. What problem are you trying to solve that requires super-big integers? – Matt Timmermans Jan 01 '17 at 16:14
-
1Please post some code, because you are likely doing something wrong that has nothing to do with adding big integers – Samuel Yvon Jan 01 '17 at 16:17
3 Answers
Is there a variable type bigger than BigInteger?
No there isn't. Biginteger uses byte arrays behind the scenes and just allocates more space for it whenever it needs it. It's size limit is the available memory.
You will probably hit the limit when you have used half of memory. At that point if it needs to grow the number it has it must first allocate a space bigger than the space it is using and run out.

- 64,482
- 16
- 119
- 213
The error occurs because JVM is running out of heap space. You can try increasing the heap size using
java -Xmx256m MyClass.java
Xmx- Max Heap Size

- 1,280
- 3
- 16
- 42
-
@OctopusProteins No just do it in command line in Windows or terminal if Linux and then execute the program . – user1613360 Jan 01 '17 at 16:15
-
Ahhh I see. Does the allocation save or is it required to redo every restart? – OctopusProteins Jan 01 '17 at 16:17
-
@OctopusProteins Its just a command. You don't need to add the code anywhere . The default maximum heap size is half of the physical memory up to a physical memory size of 192 megabytes and otherwise one fourth of the physical memory up to a physical memory size of 1 gigabyte. You can set it up in bash files so that it happens automatically . – user1613360 Jan 01 '17 at 16:18
-
It would be better if you pasted your code here, but I'll try to give an answer with what you've posted so far.
Java's BigIntegers are made as large as necessary to house the result of whatever operation you're using. BigIntegers are just like Integers without the overflow exceptions. From oracle docs:
Semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification.
Most likely you're not allocating enough memory to your java process. Not sure how you're executing your java processes, but you're looking for these arguments to be provided:
java -Xmx1024M -Xms1024M

- 46
- 3