jvm 2012-08-05

1. 生成error 文件的路径:你可以通过参数设置-XX:ErrorFile=/path/hs_error%p.log, 默认是在java运行的当前目录 [default: ./hs_err_pid%p.log]

2. 参数-XX:OnError  可以在crash退出的时候执行命令,格式是-XX:OnError=“string”,  <string> 可以是命令的集合,用分号做分隔符, 可以用"%p"来取到当前进程的ID.
例如:

// -XX:OnError="pmap %p"                // show memory map

// -XX:OnError="gcore %p; dbx - %p"     // dump core and launch debugger
在linux中系统会fork出一个子进程去执行shell的命令,因为是用fork可能会内存不够的情况,注意修改你的 /proc/sys/vm/overcommit_memory 参数,不清楚为什么这里不使用vfork

3. -XX:+ShowMessageBoxOnError 参数,当jvm crash的时候在linux里会启动gdb 去分析和调式,适合在测试环境中使用。

什么情况下不会生成error文件
linux 内核在发生OOM的时候会强制kill一些进程, 可以在/var/logs/messages中查找

 
ulimit -c unlimited
ulimit -c 0 关闭


Xloggc:${目录}/temp_gc.log           (GC日志文件)
-XX:+HeapDumpOnOutOfMemoryError       (内存溢出时生成heapdump文件)
-XX:HeapDumpPath=${目录}              (heapdump文件存放位置)

如果要即时动态生成heapdump文件可以使用jmap命令,jdk6.0已取消了-XX:+HeapDumpOnCtrlBreak配置参数通过


jmap -histo <pid> |grep DirectByteBuffer$Deallector [产看当前directbytebuffer 对象个数,不管死活















遇到问题 到 bugs.sun.com查看



1:查看gc 常用指令jstat -gcutil `pgrep -u admin java` 1s 


3:eclipse遇到报错:

The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar

google后发现用以下方式解决:
这个事eclipse的设置问题,它默认把这些受访问限制的API设成了ERROR,你只要把
Windows-Preferences-Java-Complicer-Errors/Warnings
里面的Deprecated and ricted API中的Forbidden references(access rules)选为Warning就可以编译通过了。


通过 Unsafa 方式调用 放射快
package com.alipay.com;

import java.io.Serializable;
import java.lang.reflect.Field;

import sun.misc.Unsafe;

/**
* @author haitao.yao Dec 14, 2010
*/
public class ReflectionCompare {
private static final int count = 10000;
/**
* @param args
*/
public static void main(String[] args) {
long duration = testIntCommon();
System.out.println("int common test for  " + count
+ " times, duration: " + duration);
duration = testUnsafe();
System.out.println("int unsafe test for  " + count
+ " times, duration: " + duration);
}
private static long testUnsafe() {
long start = System.currentTimeMillis();
sun.misc.Unsafe unsafe = getUnsafe();
int temp = count;
Field field = getIntField();
long offset = unsafe.objectFieldOffset(field);
while (temp-- > 0) {
unsafe.getInt(new TestBean(), offset);
}
return System.currentTimeMillis() - start;
}
private static long testIntCommon() {
long start = System.currentTimeMillis();
int temp = count;
getIntField().setAccessible(true);
while (temp-- > 0) {
TestBean bean = new TestBean();
try {
System.out.println(getIntField().get(bean));
} catch (Exception e) {
e.printStackTrace();
}
}
return System.currentTimeMillis() - start;
}
private static final sun.misc.Unsafe unsafe;
static {
sun.misc.Unsafe value = null;
try {
Class<?> clazz = Class.forName("sun.misc.Unsafe");
Field field = clazz.getDeclaredField("theUnsafe");
field.setAccessible(true);
value = (Unsafe) field.get(null);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("error to get theUnsafe", e);
}
unsafe = value;
}
public static final sun.misc.Unsafe getUnsafe() {
return unsafe;
}
private static final Field intField;
private static final Field stringField;
static {
try {
intField = TestBean.class.getDeclaredField("age");
stringField = TestBean.class.getDeclaredField("name");
} catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException("failed to init testbean field", e);
}
}
public static final Field getIntField() {
return intField;
}
public static final Field getStringField() {
return stringField;
}

/**
* @author haitao.yao
* Dec 14, 2010
*/
static class TestBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5994966479456252766L;

private String name;
private int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
}
或者http://rednaxelafx.iteye.com/blog/1102979


Bits.maxMemory

猜你喜欢

转载自cunzhangok.iteye.com/blog/1621691
JVM