Is there a simple way of obtaining all object instances of a specific class in Java

后端 未结 6 1500
终归单人心
终归单人心 2020-11-29 09:12

Currently I am working on a Java agent to assemble memory stats. With the help of the instrumentation API I can get a hold of the classes (and manipulate them). With plain J

6条回答
  •  感情败类
    2020-11-29 09:48

    When I read this I was thinking that there must be SOME way to get this kind of info, since java profilers exist. Maybe this will help: http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html. It describes the interface between the JVM and a profiler agent. But if you were actually looking to write this in Java you may be out of luck.

    Specifically, check out this function:

    jint (*EnableEvent)(jint event_type, void *arg);
    
        Called by the profiler agent to enable notification of a particular type of event. Apart from event_type, the profiler may also pass an argument that provides additional information specific to the given event type.
    
        All events are disabled when the VM starts up. Once enabled, an event stays enabled until it is explicitly disabled.
    
        This function returns JVMPI_NOT_AVAILABLE if event_type is JVMPI_EVENT_HEAP_DUMP, JVMPI_EVENT_MONITOR_DUMP or JVMPI_EVENT_OBJECT_DUMP. The profiler agent must use the RequestEvent function to request these events.
    
        Arguments:
    
            event_type  - type of event, JVMPI_EVENT_CLASS_LOAD etc.
            arg     - event specific argument.
    
        Returns:
    
            JVMPI_SUCCESS   enable succeeded.
            JVMPI_FAIL  enable failed.
            JVMPI_NOT_AVAILABLE     support for enabling the given event_type is not available. 
    

提交回复
热议问题