• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Causes for errors in JVM

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. what can cause a StackOverflowError ? (besides a function calling itself without limits)

2. what can cause a OutOfMemoryError ?

>>>Any more errors like these and how they can occur ?

 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StackOverflowError is thrown only when recursion goes too deeply.
As for OutOfMemoryError, here what's said in java docs:
"Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector."
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zandis Murāns wrote:StackOverflowError is thrown only when recursion goes too deeply.
As for OutOfMemoryError, here what's said in java docs:
"Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector."



Is JVM out of memory means it has reached the max heap size that it can use?

Does no more memory could be made by Garbage collector means when Out of memory error occurs JVM invokes Garbage Collection?
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zandis Murāns wrote:
As for OutOfMemoryError, here what's said in java docs:
"Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector."



Any SIMPLE code which will make that happen ?
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output ->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
at java.lang.StringBuilder.append(StringBuilder.java:119)
at Chumma.main(Chumma.java:9)
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i hits 20, the length of s is 27,262,976. That's in chars, so the number of bytes is twice as large, 54,525,952. That's 52MB (plus a little overhead) for that one String only. The next increase would push this to 104MB, and that's more than the 64MB the JVM gets by default.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:When i hits 20...


Mine stopped at 22. why does that happen ?

Rob Spoor wrote:the length of s is 27,262,976


What is this length & how did you get it ?
How to calculate the memory used at i=20 ?

Rob Spoor wrote:the 64MB the JVM gets by default.


How can i change that ? And why is it given just 64 Mb ? (why not 128 or 117 ?)

I tried calculating the memory used by the strings only (Please tell me if i am making errors) :
A string is of length 26*i chars. i = 1 to 20.

20
Σ (26*i) = 26*(20+1)*20/2 = 5460 char.
1

Now, size of 1 char = 2 bytes
So, total memory = 2*5460 bytes => 10920/1024 kb = 10.66 kb


PS : How to put mathematical symbols like sigma from this site itself ?


 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Sudip Bose wrote:

Rob Spoor wrote:When i hits 20...


Mine stopped at 22. why does that happen ?


Perhaps you started the JVM with more memory. When I tried to use -Xmx1500m, to give the JVM 1.5GB, it also ran to 22.

Rob Spoor wrote:the length of s is 27,262,976


What is this length & how did you get it ?
How to calculate the memory used at i=20 ?


I changed the printout to After that the bulk memory usage of the string was purely arithmetic. You can use Runtime.getRuntime() to get some basic details on actual memory, or MemoryMXBean for a bit more details information.

Rob Spoor wrote:the 64MB the JVM gets by default.


How can i change that ? And why is it given just 64 Mb ? (why not 128 or 117 ?)


The -Xmx flag I mentioned before can be used to give the JVM more memory. Because of a limitation by Windows, a 32-bit JVM cannot get more than roughly 1.5GB. A 64-bit JVM (which requires 64-bit Windows) has virtually no limit.

I tried calculating the memory used by the strings only (Please tell me if i am making errors) :
A string is of length 26*i chars. i = 1 to 20.

20
Σ (26*i) = 26*(20+1)*20/2 = 5460 char.
1

Now, size of 1 char = 2 bytes
So, total memory = 2*5460 bytes => 10920/1024 kb = 10.66 kb


s starts with 26 characters. Then it's appended to itself, for a total of 52 characters. Applied again it's 104, 208, etc. In the end, it's 26*(2^i).
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:
s starts with 26 characters. Then it's appended to itself, for a total of 52 characters. Applied again it's 104, 208, etc. In the end, it's 26*(2^i).



Oh...doh !!! Got that one ! Thanks.

Is this ok ?

20
Σ 2(1-2^20)/(1-2) = 2097150.
1

Memory = 26 * 2 * 2097150 bytes => 88080300/1024 kb => 86016 Kb => 84 Mb > 64 Mb.

PS : I wonder if you wear a mask and a cape (no offence)

 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something's not right about that sum. What are you summing about? Right now I see 20 times 2(1-2^20)/(1-2), which is the same as 20 times 2(2^20-1)/(2-1) or simply 20 times 2^21-2.

As I said before, the number of bytes for s is 2 * 26 * 2^i. The sum should be this:
20
∑ 2^i
1

The result is the same though - 2097150. Multiplied by 26*2 is not 88080300 but 109051800. The result is almost 104MB.

So it seems that your value inside the sum is right, except it should be taken outside the sum. Apparently, the following holds:
n
∑ 2^i == 2^(n + 1) - 2.
1

Interesting. And with induction you can even prove that.

Special case n == 1:
2^1 == 2^2 - 2 // math, 2 == 2, true

Assuming that this holds for n, prove for n + 1:
n+1
∑ 2^i
1
== // math
n
∑ 2^i + 2^(n + 1)
1
== // assumption
2^(n + 1) - 2 + 2^(n + 1)
== // math
2 * 2^(n + 1) - 2
== // math
2^(n + 2) - 2 // proven

These two separate proofs are enough:
- it holds for n + 1 because we assumed it holds for n
- it holds for n because we assumed it holds for n - 1
...
- it holds for 3 because we assumed it holds for 2
- it holds for 2 because we assumed it holds for 1
- we've proven it holds for 1


And as for how to add the sum symbol: use its HTML entity: ∑

And no, I don't wear a mask and cape. I sleep at night
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic