Skip to content

Commit

Permalink
Updated Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
rangareddy committed May 22, 2021
1 parent bd5a9ab commit 482f5ca
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
17 changes: 8 additions & 9 deletions array_size_exceeds.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ Requested array size exceeds VM limit indicates that indicates that the applicat
In most cases the problem is either a configuration issue (heap size too small), or a bug that results in an application attempting to create a huge array, for example, when the number of elements in the array are computed using an algorithm that computes an incorrect size.

```java
package com.ranga.oom;
// ArraySizeExceedsLimitDemo.java
// JVM Parameters: -Xms10m -Xmx10m

public class ArraySizeExceedsLimit {
package com.ranga.java.oom;

public class ArraySizeExceedsLimitDemo {
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE;
public static void main(String[] args) {
arraySizeChecker();
}

public static void arraySizeChecker(){
int[] bytes=new int[MAX_ARRAY_SIZE];
public static void main(String[] args) {
int[] bytes = new int[MAX_ARRAY_SIZE];
}
}
```
Expand All @@ -29,8 +29,7 @@ The maximum positive int in Java is 2^31 – 1 = 2,147,483,647. And the platform
$ javac ArraySizeExceedsLimitDemo.java
$ java -Xms10m -Xmx10m ArraySizeExceedsLimitDemo
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at ArraySizeExceedsLimitDemo.arraySizeChecker(ArraySizeExceedsLimitDemo.java:8)
at ArraySizeExceedsLimitDemo.main(ArraySizeExceedsLimitDemo.java:4)
at com.ranga.java.oom.ArraySizeExceedsLimitDemo.main(ArraySizeExceedsLimitDemo.java:10)
```

The java.lang.OutOfMemoryError: Requested array size exceeds VM limit can appear as a result of either of the following situations:
Expand Down
52 changes: 43 additions & 9 deletions meta_space.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,58 @@ jstat -gcmetacapacity 11236
Hence, this type of error is thrown by JVM for having a large number of big classes. The following example uses javassist package from the link: http://jboss-javassist.github.io/javassist/ which enables Java bytecode manipulation.

```java
import javassist.CannotCompileException;
// MetaspaceDemo.java
// JVM Parameters: -XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=64M
// javac -cp javassist.jar MetaspaceDemo.java
// java -cp .:javassist.jar MetaspaceDemo

package com.ranga.java.oom;

import javassist.ClassPool;

public class MetaspaceDemo {
public static void main(String args[]) throws Exception
{
public static void main(String[] args) throws Exception {
ClassPool classPool = ClassPool.getDefault();

for (int i = 0; i <100000000 ; i++) {
Class claz = classPool.makeClass("MetaspaceDemo" + i).toClass();
for (int i = 0; i < 70000; i++) {
Class claz = classPool.makeClass("MetaspaceDemo " + i).toClass();
System.out.println(claz.getName());
}
}
}
```

javac -cp javassist.jar MetaspaceDemo.java
java -cp .:javassist.jar MetaspaceDemo
```sh

$ javac -cp javassist.jar MetaspaceDemo.java
$ java -cp .:javassist.jar MetaspaceDemo

MetaspaceDemo 1
MetaspaceDemo 2
MetaspaceDemo 3
..
..
MetaspaceDemo 68664
MetaspaceDemo 68665

Exception in thread "main" javassist.CannotCompileException: by java.lang.OutOfMemoryError: Metaspace
at javassist.ClassPool.toClass(ClassPool.java:1085)
at javassist.ClassPool.toClass(ClassPool.java:1028)
at javassist.ClassPool.toClass(ClassPool.java:986)
at javassist.CtClass.toClass(CtClass.java:1079)
at com.ranga.java.oom.MetaspaceDemo.main(MetaspaceDemo.java:14)
Caused by: java.lang.OutOfMemoryError: Metaspace
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javassist.ClassPool.toClass2(ClassPool.java:1098)
at javassist.ClassPool.toClass(ClassPool.java:1079)
... 4 more
```
This code will keep generating new classes and loading their definitions to Metaspace until the space is fully utilized and the java.lang.OutOfMemoryError: Metaspace is thrown. When launched with -XX:MaxMetaspaceSize=64m then on Mac OS X my Java 1.8.0_05 dies at around 70, 000 classes loaded.

This code will keep generating new classes and loading their definitions to Metaspace until the space is fully utilized and the **java.lang.OutOfMemoryError: Metaspace** is thrown. When launched with **-XX:MaxMetaspaceSize=64m** then on Mac OS X my Java 1.8.0_05 dies at around 70,000 classes loaded.

The above code will continue to generate new classes at runtime and load their definitions into Metaspace until the space is fully utilized and java.lang.OutOfMemoryError: Metaspace is thrown.

Expand Down
19 changes: 11 additions & 8 deletions native_thread.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,22 @@ public class NativeThreadDemo {
```

```sh
javac NativeThreadDemo.java
java NativeThreadDemo
$ javac NativeThreadDemo.java
$ java NativeThreadDemo

1
2
...

4069
4070
8160
8161
8162
8163
8164
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:717)
at NativeThreadDemo.runThreads(NativeThreadDemo.java:18)
at NativeThreadDemo.main(NativeThreadDemo.java:3)
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:717)
at com.ranga.java.oom.NativeThreadDemo.main(NativeThreadDemo.java:16)
```
The native thread limit is platform-dependent so it will take a different number of threads in order to reach to the limit.

Expand Down

0 comments on commit 482f5ca

Please sign in to comment.