001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.instrument; 018 019import java.lang.instrument.Instrumentation; 020 021import org.apache.wicket.core.util.lang.WicketObjects; 022import org.apache.wicket.core.util.lang.WicketObjects.IObjectSizeOfStrategy; 023 024/** 025 * Instrumentation agent for calculating object sizes using Java's instrumentation API. To use it, 026 * have the jar somewhere we you can access it (just having this class on the classpath is not 027 * enough) and startup your application with a -javaagent argument like e.g: 028 * '-javaagent:/mydir/wicket-objectsizeof-agent-1.3-SNAPSHOT.jar'. When the application starts up, 029 * this agent will register an {@link IObjectSizeOfStrategy} at 030 * {@link WicketObjects#setObjectSizeOfStrategy(IObjectSizeOfStrategy)}. Note that this is a static 031 * registration. 032 * 033 * @author eelcohillenius 034 */ 035public class ObjectSizeOfAgent 036{ 037 038 /** 039 * Initializes agent when it is attached to an already running JVM. 040 * 041 * @param agentArgs 042 * Arguments passed in to the agent 043 * @param instrumentation 044 * The instrumentation class 045 */ 046 public static void agentmain(String agentArgs, Instrumentation instrumentation) 047 { 048 049 InstrumentationObjectSizeOfStrategy strategy = new InstrumentationObjectSizeOfStrategy( 050 instrumentation); 051 WicketObjects.setObjectSizeOfStrategy(strategy); 052 } 053 054 /** 055 * Initializes agent before the main function of the application is executed. 056 * 057 * @param agentArgs 058 * Arguments passed in to the agent 059 * @param instrumentation 060 * The instrumentation class 061 */ 062 public static void premain(String agentArgs, Instrumentation instrumentation) 063 { 064 065 InstrumentationObjectSizeOfStrategy strategy = new InstrumentationObjectSizeOfStrategy( 066 instrumentation); 067 WicketObjects.setObjectSizeOfStrategy(strategy); 068 } 069}