• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.OutOfMemoryError  RSS feed

 
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

for the following code



the output is





my doubt/question over here is, we are getting the said error(as per my understanding-correct me if am wrong)because the memory size in/or provided my JVM is not to the extent of the max int size and hence we are getting the error,but why is not the same with a normal int variable? If we are giving the value for a int say there is no error shown but if we write in or we get out of range compile time error. are not storing the max integer value in an int variable and creating an int array of the max int size same in terms of memory.



 
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try following your code through and thinking about what the program is doing.

At the moment, your code compiles. When you try to run it, you get told the JVM doesn't have enough memory for what you're asking it to do. So, what *are* you asking it to do? It's not putting the number 2147483647 into an array - that's why there's no problem with initialising a.

ETA - I may not be reading your post correctly and you may well know this. So, yannow, ignore me if I've misunderstood you.
 
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.MAX_VALUE = 2147483647 and if you try to use integer numbers greater than 2147483647 you receive java.lang.OutOfMemoryError.
To avoid errors, you must declare the variable as LONG.

 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jam Rei wrote:Integer.MAX_VALUE = 2147483647 and if you try to use integer numbers greater than 2147483647 you receive java.lang.OutOfMemoryError.
To avoid errors, you must declare the variable as LONG.

I am afraid that is completely wrong. You do not receive an out of memory error for numbers outwith the range of their type. You cannot declare the number as a long there, and it is long not LONG. I shall leave you to find out what those three errors will cause to happen.
 
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'a' is a single int, which takes up 4 bytes whatever the value it has.

'i' is an array of ints, each of which will have 4 bytes.
You have asked for space to fit 2147483647 of them.
That's 2147483647 * 4 in bytes.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jam Rei wrote:Integer.MAX_VALUE = 2147483647 and if you try to use integer numbers greater than 2147483647 you receive java.lang.OutOfMemoryError.
To avoid errors, you must declare the variable as LONG.



we will get the following error integer number too large error and not java.lang.OutOfMemoryError

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:'a' is a single int, which takes up 4 bytes whatever the value it has.

'i' is an array of ints, each of which will have 4 bytes.
You have asked for space to fit 2147483647 of them.
That's 2147483647 * 4 in bytes.


Hi

Dave

so 'a' which is a single int, is storing 2147483647 value in it. But is not same as 'i',because in the first instance if we are giving more than the intended value i.e. if we are giving 2147483648 instead of 2147483647 integer number too large error crops up and the case in 'i' we get " java.lang.OutOfMemoryError: Requested array size exceeds VM limit " are not both same... ?

 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys,

"java.lang.OutOfMemoryError" is result of Copy / Paste. It have to be Integer Out Of Range .

LONGT is written with capital letters in order to achieve greater emphasis on the data type.
... You cannot declare the number as a long there, and it is long not LONG. ...
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:
Jam Rei wrote:Integer.MAX_VALUE = 2147483647 and if you try to use integer numbers greater than 2147483647 you receive java.lang.OutOfMemoryError.
To avoid errors, you must declare the variable as LONG.



we will get the following error integer number too large error and not java.lang.OutOfMemoryError

That is if you write something like 2345678910 as an int literal. If you use a long, you will suffer a different error. Have you tried it?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jam Rei wrote:Sorry guys,

"java.lang.OutOfMemoryError" is result of Copy / Paste. It have to be Integer Out Of Range.

LONGT is written with capital letters in order to achieve greater emphasis on the data type.
Apart from the spelling error, that is still wrong. You will have to get used to the fact that Java® keywords are all lower‑case.
You are still mistaken about the int; 2147483647 is a normal int literal and its use will cause no problems. You are not helping the OP understand his question.
 
Dave Tolls
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:if we are giving 2147483648 instead of 2147483647 integer number too large error crops up and the case in 'i' we get " java.lang.OutOfMemoryError: Requested array size exceeds VM limit " are not both same... ?



These are two different things.
2147483647 is the maximum value of an int.  The compiler will complain as it is not an int, it's too big.

In the second instance it's a runtime exception caused by attempting to create an array that is bigger than the available memory (hence it being an OutOfMemoryError).
In this case you are asking the JVM to claim some 8Gb of heap space (give or take).
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:. . . are not both same... ?
DT has explained that the two occurrences are different. In one case, the code fails to compile. In the other, it compiles and starts running, only to fail with the out of memory error.

If you manage to run anything after the compiler error, the old version of the .class file will still exist in memory and you can run its methods.
javac MyClass.java
Number outwith range for int MyClass line 12
2147483648
^
1 error
java MyClass
2147483647
Exception in Thread main: java.lang.OutOfMemoryError ...
When you run the methods of MyClass, you have the previous version, and it does not mean that the code you wrote can actually be executed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!