我使用了物品http://www.devinline.com/2016/05/java-instrumentation-fundamental-part-1.html?m=1使用儀器測量物體的大小
我需要獲取查詢結果的大小。
的
long sizeOfObject = InstrumentationAgent.findSizeOfObject(myvar);
調用但是返回錯誤
代理不initted。
我有拋出類的主要方法Exception
你可以推薦正確的語法嗎?
修訂: 代理代碼:
package org.h2.command;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
public class InstrumentationAgent {
/*
* System classloader after loading the Agent Class, invokes the premain
* (premain is roughly equal to main method for normal Java classes)
*/
private static volatile Instrumentation instrumentation;
public static void premain(String agentArgs, Instrumentation instObj) {
// instObj is handle passed by JVM
instrumentation = instObj;
}
public static void agentmain(String agentArgs, Instrumentation instObj)
throws ClassNotFoundException, UnmodifiableClassException {
}
public static long findSizeOfObject(Object obj) {
// use instrumentation to find size of object obj
if (instrumentation == null) {
throw new IllegalStateException("Agent not initted");
} else {
return instrumentation.getObjectSize(obj);
}
}
}
我調用:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import org.h2.command.InstrumentationAgent;
import static java.lang.System.out;
public class CacheOptimize {
public long Size;
public static void main(String[] args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:file:D:/server/h2/exp1.h2.db", "sa", "sa");
Statement stat = conn.createStatement();
ResultSet rs;
rs = stat.executeQuery("select * from TAbles");
Size = InstrumentationAgent.findSizeOfObject(rs);
}
stat.close();
conn.close();
}
顯示您的'MANIFEST.MF'和代理商代碼 – vsminkov
添加nessasary代碼 –
任何人都可以幫忙嗎? –