java – 使用检测对象的大小

我使用了文章
http://www.devinline.com/2016/05/java-instrumentation-fundamental-part-1.html?m=1

我需要获得查询结果的大小.

但是呼唤

long sizeOfObject = InstrumentationAgent.findSizeOfObject(myvar);

返回错误

Agent not initted.

我有抛出异常的类的主要方法
你能给出正确语法的建议吗?

修订:
代理代码:

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();
}

最佳答案 您要么忘了添加META-INF / MANIFEST.MF,请输入

Premain-Class: org.h2.command.InstrumentationAgent

或运行您的应用程序而不使用-javaagent:path / to / agent.jar.

Here您可以找到如何使用代理运行启动应用程序的完整工作示例.

您还可以在official javadoc中找到有关清单条目和运行代理的更多信息.

注意

您似乎正在尝试获取ResultSet返回的数据大小,但不会尝试ResultSet对象本身消耗的内存量.问题是

size = InstrumentationAgent.findSizeOfObject(rs);

将不是最佳方法,因为ResultSet仅将游标维护到数据库行,并且不存储所有结果.但是,您可以使用它获取所有数据并使用findSizeOfObject汇总大小.但你应该知道的最后一件事是Instrumentation#getObjectSize可能会返回不准确的结果

Returns an implementation-specific approximation of the amount of storage consumed by the specified object. The result may include some or all of the object’s overhead, and thus is useful for comparison within an implementation but not between implementations. The estimate may change during a single invocation of the JVM.

点赞