java -server
-XX:MaxDirectMemorySize=2g # 直接内存的大小为 2GB
-Xms3g -Xmx3g # Java 堆内存的大小为 3GB
-XX:NewSize=1g # 新生代的大小为 1GB
-XX:MetaspaceSize=128m # 元空间为 128MB
-XX: UseParNewGC -XX: UseConcMarkSweepGC # 新生代使用 ParNewGC,老年代使用 CMS
-xx: HeapDumponCutOfMemoryError # 在发生 OOM 时打印日志
-XX:HeapDumpPath=dump.log # OOM 日志存储地址
-XX: PrintGC # 输出 GC 日志
-XX: PrintGCDetails # 输出 GC 的详细日志
-XX: PrintGCDatestamps # 输出 GC 的时间戳
-XX: PrintHeapAtGC # JVM 在执行 GC 操作的前后打印堆的信息
-Xlogge:../gc/gc.log # GC日志的输出地址
-jar start.jar
class Myresources1 implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("资源1关闭方法执行");
throw new Exception("资源1关闭异常");
}
}
class laipuhuo.com Myresources2 implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("资源2关闭方法执行");
throw new Exception(laipuhuo.com "资源2关闭异常");
}
}
Myresources1 myresources1 =null;
Myresources2 myresources2 = null;
try{
myresources1 = new Myresources1();
myresources2 = new Myresources2();
System.out.println("hello");
}catch (Exception e){
e.printStackTrace();
}finally {
if (myresources1!=null){
try {
myresources1.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if (myresources2!=null){
try {laipuhuo.com
myresources2.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
Myresources1 myresources1 = null;
try{
myresources1 = new Myresources1();
//算数异常laipuhuo.com
System.out.println(10/0);
}finally {
if (myresources1!=null)
myresources1.close();
}
//myresources1类
class Myresources1 implements AutoCloseable {
@Override
public void close(laipuhuo.com) throws Exception {
System.out.println("资源1关闭方法执行");
throw new Exception("资源1关闭异常");
}
} try(FileWriter fw = new FileWriter("a.txt")){
fw = new FileWriter("b.txt"); //报错
}catch (Exception e){
e.printStackTrace(laipuhuo.com
);
}
//此时会编译出错:The resource is1 of a try-with-resources statement cannot be assigned