Auflistung aller geladenen Klassen und Ermittlung Platformspezifischer Objektgroessen

Thomas Darimont

Erfahrenes Mitglied
Hallo!

Die Sun JVM bietet ab der Version 5 die Moeglichkeit spezielle Agenten beim Start der JVM einzuschleusen ( BEAs JRockit JVM erlaubt das schon laenger...).
Diese erlauben es beispielsweise ueber spezielle ClassFileTransformer den bytecode von Klassen beim Laden
zu modifizieren. Die AspectJ Laufzeit verwendet diesen Trick beispielsweise um das Load-time Weaving von
Aspekten zu ermoeglichen.

Neben der Manipulation erlaubt Instrumentation jedoch auch die Auflistung aller geladenen Klassen
und die Bestimmung platformspezifischer Objektgroessen (Speicherverbrauch in bytes) zur Laufzeit.

Hier mal ein kleines Beispiel:

Wir erzeugen uns einen einfachen Java Agenten:
Java:
/**
 * 
 */
package de.tutorials;

import java.lang.instrument.Instrumentation;

/**
 * @author Tom
 *
 */
public class SimpleJavaAgent {
    
    private static Instrumentation instrumentation; 
    
    public static void premain(String agentArgs, Instrumentation inst){
        instrumentation = inst;
    }

    public static Instrumentation getInstrumentation() {
        return instrumentation;
    }
}

Diese Klasse legen wir in einem jar namens simpleAgent.jar mit folgendem Manifest ab:
Code:
Manifest-Version: 1.0
Premain-Class: de.tutorials.SimpleJavaAgent
Can-Redefine-Classes: true

Hier verwenden wir unseren JavaAgenten...
Java:
/**
 * 
 */
package de.tutorials;

/**
 * @author Tom
 * 
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        Class[] loadedClasses = SimpleJavaAgent.getInstrumentation()
                .getAllLoadedClasses();
        for (Class clazz : loadedClasses) {
            System.out.println(clazz);
        }

        System.out.println("Object sizes...");
        Object o = new Object();
        System.out.println(SimpleJavaAgent.getInstrumentation()
                .getObjectSize(o));
        
        Object oo = new Object() {
            Object o;
        };
        System.out.println(SimpleJavaAgent.getInstrumentation().getObjectSize(
                oo));
    }
}

Anschliessend starten wir den Java launcher mit unserem Agenten:
java -cp bin -javaagent:lib/simpleAgent.jar de.tutorials.Main

...und erhalten folgende Ausgabe:
Code:
class sun.nio.cs.Surrogate
class java.util.Collections$ReverseComparator
class sun.instrument.TransformerManager
class java.nio.charset.CharsetEncoder
class sun.nio.cs.MS1252
class java.io.FilterOutputStream
class java.lang.reflect.ReflectPermission
class sun.misc.Launcher$ExtClassLoader$1
class java.lang.CharacterDataLatin1
class sun.misc.Launcher$AppClassLoader
class java.util.Hashtable$Entry
class [Ljava.util.Hashtable$Entry;
class sun.misc.SoftCache
class java.lang.Math
class java.lang.ArrayStoreException
class java.util.Hashtable
interface java.util.RandomAccess
interface java.lang.Runnable
class [Ljava.lang.Runnable;
class java.lang.Object
class [Ljava.lang.Object;
class [[Ljava.lang.Object;
class java.nio.HeapByteBuffer
interface java.io.DataInput
class java.lang.Compiler$1
class sun.reflect.ConstantPool
class java.util.jar.JarEntry
class java.util.Stack
class java.lang.ref.ReferenceQueue
class sun.misc.ExtensionDependency
class sun.nio.cs.StandardCharsets$Aliases
class sun.net.www.ParseUtil
class sun.io.Converters
interface java.lang.reflect.Type
class [Ljava.lang.reflect.Type;
class java.lang.OutOfMemoryError
class java.util.Hashtable$EmptyEnumerator
class java.util.Random
class java.net.UnknownContentHandler
class java.util.zip.ZipFile$2
class sun.misc.Launcher$AppClassLoader$1
class java.security.UnresolvedPermission
class java.lang.reflect.ReflectAccess
class sun.instrument.InstrumentationImpl
interface sun.nio.cs.HistoricallyNamedCharset
class java.util.zip.ZipFile$ZipFileInputStream
class sun.misc.JarIndex
interface sun.reflect.LangReflectAccess
class java.lang.Class$3
class java.util.AbstractList
interface java.net.URLStreamHandlerFactory
class java.lang.Throwable
class sun.misc.Launcher$Factory
class java.lang.System
class sun.net.www.protocol.jar.Handler
class java.security.Permission
class java.lang.ref.PhantomReference
class java.net.URLClassLoader
interface java.lang.Appendable
class java.util.jar.JavaUtilJarAccessImpl
class java.io.FilterInputStream
class java.lang.Float
class sun.misc.Signal
class java.lang.ArithmeticException
class sun.misc.AtomicLong
class java.io.ExpiringCache$Entry
class sun.misc.Version
class java.util.Collections$EmptyList
class sun.misc.VM
class java.util.Collections$EmptyMap
class java.util.zip.Inflater
class java.lang.Terminator$1
class java.lang.Long
class java.nio.Buffer
class java.util.BitSet
interface java.security.Principal
class [Ljava.security.Principal;
class java.util.HashMap$Entry
class [Ljava.util.HashMap$Entry;
class sun.security.action.GetPropertyAction
class java.lang.NullPointerException
class java.security.AllPermission
class java.lang.reflect.Modifier
interface java.lang.CharSequence
class [Ljava.lang.CharSequence;
class java.util.jar.JarFile
class java.util.Collections$SynchronizedMap
class java.lang.ClassLoader$3
interface java.security.Guard
class java.lang.StrictMath
class java.lang.ref.Reference$Lock
interface java.util.Map$Entry
class [Ljava.util.Map$Entry;
class java.net.Parts
class java.net.URL
class [Ljava.net.URL;
class java.io.File
class [Ljava.io.File;
class java.io.PrintStream
class java.nio.CharBuffer
class java.lang.Class
class [Ljava.lang.Class;
class java.io.ObjectStreamField
class [Ljava.io.ObjectStreamField;
interface java.lang.Readable
class sun.misc.AtomicLongCSImpl
class java.lang.ref.ReferenceQueue$Null
class java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceF
class java.io.FileSystem
class sun.nio.cs.StandardCharsets$Cache
class java.nio.Bits
interface sun.reflect.ConstructorAccessor
class sun.misc.URLClassPath$FileLoader$1
class sun.nio.cs.StreamEncoder$CharsetSE
class java.lang.management.MemoryUsage
class java.lang.ClassLoader
interface sun.misc.SignalHandler
class sun.util.PreHashedMap
class java.util.LinkedHashMap$Entry
class java.lang.StringCoding$StringDecoder
class java.io.FilePermission$1
class java.lang.ThreadGroup
class [Ljava.lang.ThreadGroup;
class java.lang.Double
class java.lang.ref.Finalizer
class java.lang.Short
class sun.reflect.DelegatingMethodAccessorImpl
class java.nio.charset.spi.CharsetProvider
class java.lang.StringCoding
class java.lang.NoSuchMethodError
class java.lang.ref.Reference
class [Ljava.lang.ref.Reference;
class [[Ljava.lang.ref.Reference;
class java.lang.VirtualMachineError
class java.util.Collections$EmptySet
class java.lang.LinkageError
class java.lang.Byte
class java.util.Vector
class java.util.AbstractSet
class java.lang.reflect.Constructor
class [Ljava.lang.reflect.Constructor;
class java.lang.StringBuilder
class sun.nio.cs.StreamEncoder
class java.nio.charset.CoderResult$1
class java.net.ContentHandler
class java.util.jar.JarFile$JarFileEntry
class java.nio.ByteOrder
class java.lang.ref.ReferenceQueue$Lock
interface sun.reflect.MethodAccessor
class de.tutorials.SimpleJavaAgent
class java.util.Properties
class java.security.PermissionCollection
class java.nio.charset.CoderResult
interface java.lang.reflect.GenericDeclaration
class [Ljava.lang.reflect.GenericDeclaration;
class java.lang.ThreadLocal
class sun.reflect.Reflection
class sun.reflect.MethodAccessorImpl
class java.lang.StringBuffer
class java.lang.Number
class java.lang.reflect.Method
class [Ljava.lang.reflect.Method;
class java.lang.System$2
class java.util.zip.InflaterInputStream
class sun.nio.cs.SingleByteEncoder
class java.lang.String
class [Ljava.lang.String;
class java.nio.charset.CharsetDecoder
class java.lang.Error
class sun.reflect.NativeConstructorAccessorImpl
interface java.lang.instrument.ClassFileTransformer
class [Ljava.lang.instrument.ClassFileTransformer;
class sun.reflect.MagicAccessorImpl
class sun.nio.cs.SingleByteDecoder
class java.lang.Exception
class java.security.BasicPermissionCollection
class java.io.ExpiringCache
class java.io.ObjectStreamClass
class java.nio.charset.CodingErrorAction
class java.lang.ClassNotFoundException
class java.util.StringTokenizer
class java.io.OutputStreamWriter
interface sun.misc.JavaLangAccess
class java.security.AccessControlContext
class java.util.ArrayList
class java.security.Permissions
interface java.lang.instrument.Instrumentation
interface java.security.PrivilegedAction
class java.security.BasicPermission
class java.lang.ThreadLocal$ThreadLocalMap
class java.lang.IncompatibleClassChangeError
class java.util.Dictionary
class java.lang.AbstractStringBuilder
class java.lang.ref.FinalReference
class java.lang.String$CaseInsensitiveComparator
class java.io.OutputStream
class sun.reflect.ReflectionFactory$GetReflectionFactoryAction
class java.io.BufferedWriter
class sun.misc.FileURLMapper
class java.util.Locale
class java.security.CodeSource
class java.util.LinkedHashMap
class java.lang.RuntimePermission
interface java.io.Serializable
class [Ljava.io.Serializable;
class java.lang.InternalError
class java.lang.ref.SoftReference
class [Ljava.lang.ref.SoftReference;
class [[Ljava.lang.ref.SoftReference;
class java.lang.Package
class java.lang.ref.Finalizer$FinalizerThread
class sun.nio.cs.MS1252$Decoder
interface java.util.Map
class java.io.Win32FileSystem
class sun.misc.URLClassPath$FileLoader
interface java.util.Comparator
interface java.lang.Iterable
class java.nio.charset.CoderResult$Cache
class sun.misc.URLClassPath
class java.lang.reflect.AccessibleObject
class [Ljava.lang.reflect.AccessibleObject;
class java.lang.Terminator
class java.lang.Integer
class java.lang.ref.WeakReference
class [Ljava.lang.ref.WeakReference;
interface sun.misc.JavaUtilJarAccess
class java.net.URLClassLoader$1
class java.util.zip.ZipEntry
class java.security.PrivilegedActionException
class sun.reflect.DelegatingConstructorAccessorImpl
interface java.lang.reflect.Member
class [Ljava.lang.reflect.Member;
class java.security.AccessController
class java.lang.ClassCastException
class java.security.SecureClassLoader
interface java.lang.Cloneable
class [Ljava.lang.Cloneable;
class java.io.WinNTFileSystem
class java.security.cert.Certificate
class [Ljava.security.cert.Certificate;
class java.lang.ClassLoader$NativeLibrary
class sun.misc.Unsafe
class sun.security.util.Debug
interface java.lang.annotation.Annotation
class [Ljava.lang.annotation.Annotation;
class java.lang.Character
interface java.util.Enumeration
class java.lang.ThreadLocal$ThreadLocalMap$Entry
class [Ljava.lang.ThreadLocal$ThreadLocalMap$Entry;
class java.net.URLStreamHandler
interface java.util.zip.ZipConstants
class sun.misc.Resource
class java.io.InputStream
class java.lang.ThreadDeath
interface java.io.Closeable
class java.lang.Boolean
interface java.util.Collection
class java.io.FilePermission
class java.io.ExpiringCache$1
class sun.nio.cs.FastCharsetProvider
class java.util.AbstractCollection
class sun.reflect.DelegatingClassLoader
class java.lang.reflect.Field
class [Ljava.lang.reflect.Field;
class java.util.concurrent.atomic.AtomicReferenceFieldUpdater
class java.lang.NoClassDefFoundError
class java.io.FileInputStream
class java.lang.ref.Reference$ReferenceHandler
class sun.reflect.ReflectionFactory$1
interface java.io.Flushable
class java.lang.RuntimeException
class java.security.ProtectionDomain
class java.lang.StackTraceElement
class [Ljava.lang.StackTraceElement;
class java.util.AbstractMap
interface java.lang.Comparable
class [Ljava.lang.Comparable;
interface java.lang.Thread$UncaughtExceptionHandler
class [Ljava.lang.Thread$UncaughtExceptionHandler;
class sun.misc.Launcher$ExtClassLoader
interface sun.nio.ByteBuffered
class java.lang.StackOverflowError
interface java.util.Iterator
class sun.net.www.protocol.file.Handler
class sun.nio.cs.StandardCharsets
interface java.security.PrivilegedExceptionAction
class java.io.BufferedInputStream
class sun.misc.URLClassPath$3
class java.io.BufferedOutputStream
class java.util.zip.ZipFile
class sun.reflect.misc.ReflectUtil
class java.io.FilePermissionCollection
class sun.misc.Launcher
class java.io.FileOutputStream
class sun.misc.URLClassPath$Loader
class sun.misc.URLClassPath$JarLoader
class sun.net.www.MessageHeader
class java.nio.ByteBuffer
class sun.reflect.NativeMethodAccessorImpl
class java.util.Collections
class java.util.Hashtable$EmptyIterator
class sun.reflect.ConstructorAccessorImpl
class java.lang.SystemClassLoaderAction
class sun.reflect.ReflectionFactory
class java.net.URLConnection
class java.nio.charset.CoderResult$2
class sun.nio.cs.StandardCharsets$Classes
class java.util.concurrent.atomic.AtomicLong
interface java.lang.reflect.AnnotatedElement
class [Ljava.lang.reflect.AnnotatedElement;
class java.io.FileDescriptor
class sun.misc.NativeSignalHandler
class java.util.HashMap
class java.lang.Runtime
class java.lang.StringCoding$CharsetSD
class java.io.Writer
class java.util.HashSet
class java.lang.Class$1
class sun.net.www.protocol.file.FileURLConnection
class de.tutorials.Main
class java.nio.HeapCharBuffer
class sun.nio.cs.MS1252$Encoder
interface java.util.Set
class sun.misc.SharedSecrets
class java.lang.Thread
class [Ljava.lang.Thread;
interface java.util.List
class java.io.DataInputStream
class java.lang.Compiler
class java.nio.charset.Charset
class sun.nio.cs.Surrogate$Parser
class sun.net.www.URLConnection
class [Z
class [B
class [C
class [I
class [S
class [J
class [F
class [D
Object sizes...
8
16

Gruss Tom
 
Zurück