Skip to content

Commit

Permalink
feat: 更新 java 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
dunwu committed Sep 2, 2024
1 parent de6e74c commit d4a6433
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.dunwu.javacore.bytecode;

public interface BaseInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.dunwu.javacore.concurrent.error;

import java.util.HashMap;
import java.util.Map;

public class WrongInit {

private Map<Integer, String> students;

public WrongInit() {
new Thread(() -> {
students = new HashMap<>();
students.put(1, "王小美");
students.put(2, "钱二宝");
students.put(3, "周三");
students.put(4, "赵四");
}).start();
}

public Map<Integer, String> getStudents() {
return students;
}

public static void main(String[] args) throws InterruptedException {
WrongInit demo = new WrongInit();
System.out.println(demo.getStudents().get(1));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.dunwu.javacore.concurrent.error;

import io.github.dunwu.javacore.concurrent.annotation.Error;

@Error
public class WrongResult {

volatile static int i;

public static void main(String[] args) throws InterruptedException {
Runnable r = () -> {
for (int j = 0; j < 10000; j++) {
i++;
}
};

Thread thread1 = new Thread(r);
thread1.start();
Thread thread2 = new Thread(r);
thread2.start();
thread1.join();
thread2.join();
System.out.println(i);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.dunwu.javacore.jvm.error;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
* VM Args:-Xmx20M -XX:MaxDirectMemorySize=10M
*
* @author zzm
*/
public class DirectMemoryOOM {

private static final int _1MB = 1024 * 1024;

public static void main(String[] args) throws Exception {
Field unsafeField = Unsafe.class.getDeclaredFields()[0];
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
while (true) {
unsafe.allocateMemory(_1MB);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.dunwu.javacore.jvm.error;

import java.util.HashSet;
import java.util.Set;

/**
* VM Args:-XX:PermSize=6M -XX:MaxPermSize=6M
*
* @author zzm
*/
public class RuntimeConstantPoolOOM_1 {

public static void main(String[] args) {
// 使用Set保持着常量池引用,避免Full GC回收常量池行为
Set<String> set = new HashSet<String>();
// 在short范围内足以让6MB的PermSize产生OOM了
short i = 0;
while (true) {
set.add(String.valueOf(i++).intern());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.dunwu.javacore.jvm.error;

public class RuntimeConstantPoolOOM_2 {

public static void main(String[] args) {
String str1 = new StringBuilder("计算机").append("软件").toString();
System.out.println(str1.intern() == str1);

String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2.intern() == str2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.dunwu.javacore.jvm.error;

/**
* VM Args:-Xss2M (这时候不妨设大些,请在32位系统下运行)
*
* @author zzm
*/
public class VMStackOOM {

private void dontStop() {
while (true) {
}
}

public void stackLeakByThread() {
while (true) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
dontStop();
}
});
thread.start();
}
}

public static void main(String[] args) throws Throwable {
VMStackOOM oom = new VMStackOOM();
oom.stackLeakByThread();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
* <p>
* VM Args:-verbose:gc -Xmx20M -XX:MaxDirectMemorySize=10M
* <p>
* Linux Test Cli: nohup java -verbose:gc -Xmx20M -XX:MaxDirectMemorySize=10M -classpath "target/javacore-jvm-1.0.1.jar:target/lib/*"
* io.github.dunwu.javacore.jvm.memory.DirectOutOfMemoryDemo >> output.log 2>&1 &
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
* <p>
* VM Args: -Xms10M -Xmx10M
* <p>
* Linux Test Cli: nohup java -verbose:gc -Xms10M -Xmx10M -XX:+HeapDumpOnOutOfMemoryError -classpath
* "target/javacore-jvm-1.0.1.jar:target/lib/*" io.github.dunwu.javacore.jvm.memory.GcOverheadLimitExceededDemo >>
* output.log 2>&1 &
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-25
*/
public class GcOverheadLimitExceededDemo {
public class GcOverheadLimitExceededOOM {

public static void main(String[] args) {
List<Double> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
* <p>
* VM Args:-verbose:gc -Xms10M -Xmx10M -XX:+HeapDumpOnOutOfMemoryError
* <p>
* Linux Test Cli: nohup java -verbose:gc -Xms10M -Xmx10M -XX:+HeapDumpOnOutOfMemoryError -classpath
* "target/javacore-jvm-1.0.1.jar:target/lib/*" io.github.dunwu.javacore.jvm.memory.HeapMemoryLeakMemoryErrorDemo >>
* output.log 2>&1 &
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-25
*/
public class HeapMemoryLeakMemoryErrorDemo {
public class HeapMemoryLeakOOM {

public static void main(String[] args) {
List<OomObject> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2020-03-09
*/
public class HeapMemoryLeakMemoryErrorDemo2 {
public class HeapMemoryLeakOOM2 {

public static void main(String[] args) {
HeapMemoryLeakMemoryErrorDemo2 demo = new HeapMemoryLeakMemoryErrorDemo2();
HeapMemoryLeakOOM2 demo = new HeapMemoryLeakOOM2();
System.out.println("往ArrayList中加入30w内容");
demo.javaHeapSpace(300000);
demo.memoryTotal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
* <p>
* VM Args:-verbose:gc -Xms10M -Xmx10M
* <p>
* Linux Test Cli: java -verbose:gc -Xms10M -Xmx10M -cp target/javacore-jvm-1.0.1.jar io.github.dunwu.javacore.jvm.memory.HeapOutOfMemoryErrorDemo
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-25
*/
public class HeapOutOfMemoryErrorDemo {
public class HeapOutOfMemoryOOM {

public static void main(String[] args) {
Double[] array = new Double[999999999];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
* <li>(JDK8 以前)-XX:PermSize=10m -XX:MaxPermSize=10m</li>
* <li>(JDK8 及以后)-XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m</li>
* <p>
* Linux Test Cli: nohup java -verbose:gc -XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m -XX:+HeapDumpOnOutOfMemoryError
* -classpath "target/javacore-jvm-1.0.1.jar:target/lib/*" io.github.dunwu.javacore.jvm.memory.MethodAreaOutOfMemoryDemo
* >> output.log 2>&1 &
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-26
*/
public class MethodAreaOutOfMemoryDemo {
public class MethodAreaOOM {

public static void main(String[] args) {
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
* <li>-Xmx100M -XX:MaxPermSize=16M (JDK8 以前版本)</li>
* <li>-Xmx100M -XX:MaxMetaspaceSize=16M (JDK8 及以后版本)</li>
* </ul>
* Linux Test Cli: nohup java -verbose:gc -Xmx100M -XX:MaxMetaspaceSize=16M -XX:+HeapDumpOnOutOfMemoryError -classpath "target/javacore-jvm-1.0.1.jar:target/lib/*" io.github.dunwu.javacore.jvm.memory.PermOutOfMemoryErrorDemo >> output.log 2>&1 &
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2020-03-08
*/
public class PermOutOfMemoryErrorDemo {
public class PermGenSpaceOOM {

public static void main(String[] args) throws Exception {
for (int i = 0; i < 100_000_000; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

/**
* 创建线程导致内存溢出(执行要慎重)
* <p>
*
* VM Args: -Xss512k
* <p>
* Linux Test Cli: nohup java -verbose:gc -Xss512k -cp target/javacore-jvm-1.0.1.jar io.github.dunwu.javacore.jvm.memory.StackOutOfMemoryDemo >> output.log 2>&1 &
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-25
*/
public class StackOutOfMemoryDemo {
public class StackOutOfMemoryError {

private volatile int count;

public static void main(String[] args) {
StackOutOfMemoryDemo stackOutOfMemoryDemo = new StackOutOfMemoryDemo();
stackOutOfMemoryDemo.stackLeakByThread();
StackOutOfMemoryError demo = new StackOutOfMemoryError();
demo.stackLeakByThread();
}

public void stackLeakByThread() {
while (true) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
StackOutOfMemoryDemo.this.dontStop();
StackOutOfMemoryError.this.dontStop();
}
});
thread.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,28 @@
* <p>
* 如果没有一个新的栈帧所需空间,Java 就会抛出 StackOverflowError。
* <p>
* VM 参数:
* <ul>
* <li>-Xss228k - 设置栈大小为 228k</li>
* </ul>
* <p>
* Linux Test Cli: nohup java -verbose:gc -Xss228k -cp target/javacore-jvm-1.0.1.jar io.github.dunwu.javacore.jvm.memory.StackOverflowDemo >> output.log 2>&1 &
* VM 参数:-Xss228k - 设置栈大小为 228k
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-25
*/
public class StackOverflowDemo {
public class StackOverflowErrorDemo {

private int stackLength = 1;

public void stackLeak() {
stackLength++;
stackLeak();
}

public static void main(String[] args) {
StackOverflowDemo obj = new StackOverflowDemo();
StackOverflowErrorDemo demo = new StackOverflowErrorDemo();
try {
obj.recursion();
demo.stackLeak();
} catch (Throwable e) {
System.out.println("栈深度:" + obj.stackLength);
System.out.println("栈深度:" + demo.stackLength);
e.printStackTrace();
}
}

public void recursion() {
stackLength++;
recursion();
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package io.github.dunwu.javacore.jvm.memory;

/**
* 类成员循环依赖,导致 StackOverflowError
* <p>
* 类成员循环依赖,导致 StackOverflowError
*
* VM 参数:
* <ul>
* <li>-Xss228k - 设置栈大小为 228k</li>
* </ul>
* <p>
* Linux Test Cli: nohup java -verbose:gc -Xss228k -cp target/javacore-jvm-1.0.1.jar io.github.dunwu.javacore.jvm.memory.StackOverflowDemo2 >> output.log 2>&1 &
*
* -Xss228k - 设置栈大小为 228k
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2019-06-25
*/
public class StackOverflowDemo2 {
public class StackOverflowErrorDemo2 {

public static void main(String[] args) {
A obj = new A();
Expand Down
Loading

0 comments on commit d4a6433

Please sign in to comment.