{"id":230239,"date":"2014-07-21T13:18:03","date_gmt":"2014-07-21T09:18:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=230239"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=230239","title":{"rendered":"<span class=\"post_title\">Java Agent \u043d\u0430 \u0441\u043b\u0443\u0436\u0431\u0435 JVM<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/0d6\/cea\/505\/0d6cea5059f39fac1c6522285bd73794.png\" align=\"left\"\/><br \/>  \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u043b\u044b\u0448\u0430\u043b\u0438 \u0438\u043b\u0438 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441 \u0442\u0430\u043a\u0438\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c JVM \u043a\u0430\u043a -javaagent, \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"http:\/\/zeroturnaround.com\/software\/jrebel\">Jrebel<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/plumbr.eu\">Plumbr<\/a> \u044d\u0442\u043e \u043c\u043e\u0433\u043b\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a <code>JAVA_OPTS=-javaagent:[path\/to\/]jrebel.jar<\/code> \u0438\u043b\u0438 \u0442\u0430\u043a <code>-javaagent:\/path-to\/plumbr.jar<\/code><br \/>  \u0425\u043e\u0442\u044f <b>javaagent<\/b> \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0435\u0449\u0435 \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 java 1.5, \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0442\u0430\u043a \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0430\u0433\u0435\u043d\u0442\u043e\u0432 \u0438 \u0438\u043c\u0435\u044e\u0442 \u0441\u043c\u0443\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435.<br \/>  \u0427\u0442\u043e \u0436\u0435 \u044d\u0442\u043e \u0437\u0430 \u0430\u0433\u0435\u043d\u0442? \u0417\u0430\u0447\u0435\u043c \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0438 \u043a\u0430\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439?<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h1>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 javaagent<\/h1>\n<p>\u041a\u0430\u043a \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0432\u044b\u0448\u0435 javaagent \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 JVM, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0430\u0433\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0441 \u0432\u0430\u0448\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c, \u0430 \u0442\u043e\u0447\u043d\u0435\u0435 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0435\u0449\u0435 \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0421\u0430\u043c \u0430\u0433\u0435\u043d\u0442 \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0443 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0431\u0430\u0439\u0442-\u043a\u043e\u0434\u043e\u043c (<code>java.lang.instrument<\/code>) \u0432 runtime. \u042d\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u043a\u0440\u0430\u0442\u0446\u0435. \u041e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/instrument\/package-summary.html\">\u0442\u0443\u0442<\/a>, \u043d\u043e \u043e\u043d\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043a\u0443\u0434\u043d\u0430\u044f. \u041d\u0438\u0447\u0435\u0433\u043e \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e? \u0418\u0442\u0430\u043a, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f. \u041b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445.<\/p>\n<h2>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0439 \u0430\u0433\u0435\u043d\u0442<\/h2>\n<p>  <\/p>\n<pre><code class=\"java\">package ru.habrahabr.agent;  public class Agent007 {     public static void premain(String args) {         System.out.println(&quot;Hello! I`m java agent&quot;);             } } <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0430\u0433\u0435\u043d\u0442 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <b>premain<\/b> \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u043e\u0439<br \/>  <code>public static void premain(String args); <\/code><br \/>  \u0438\u043b\u0438 <br \/>  <code>public static void premain(String args, Instrumentation inst);<\/code><\/p>\n<p>  \u041a\u043b\u0430\u0441\u0441 \u0430\u0433\u0435\u043d\u0442\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u043d \u0432 jar \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c <code>MANIFEST.MF<\/code>, \u0441 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c<br \/>  <b>PreMain-Class<\/b> \u2014 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043a\u043b\u0430\u0441\u0441 \u0430\u0433\u0435\u043d\u0442\u0430 \u0441 premain \u043c\u0435\u0442\u043e\u0434\u043e\u043c. \u0415\u0441\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0430\u0433\u0435\u043d\u0442\u0430, \u043d\u043e \u043e\u043d\u0438 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0438 \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f.<\/p>\n<p>  \u0412\u043e\u0442 \u0442\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043d\u0430\u0448 manifest.mf.  <\/p>\n<pre><code class=\"java\">Manifest-Version: 1.0 PreMain-Class: ru.habrahabr.agent.Agent007  <\/code><\/pre>\n<p><i>\u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0444\u0430\u0439\u043b\u0430<\/i><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0443\u043f\u0430\u043a\u0443\u0435\u043c \u0432\u0441\u0435 \u044d\u0442\u043e \u0432 jar  <\/p>\n<pre><code>jar -cvfm Agent007.jar manifest.mf  ru\/habrahabr\/agent\/Agent007.class<\/code><\/pre>\n<p>  \u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043a\u043b\u0430\u0441\u0441 \u0438\u0441\u043f\u044b\u0442\u0430\u0442\u0435\u043b\u044c  <\/p>\n<pre><code class=\"java\">package ru.habrahabr.agent;  public class AgentTester { \tpublic static void main(String[] args) { \t\tSystem.out.println(&quot;Hello! I`m agent tester&quot;); \t} } <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c AgentTester \u0438\u0437 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438  <\/p>\n<pre><code class=\"bash\">java -javaagent:Agent007.jar ru.habrahabr.agent.AgentTester Hello! I`m java agent Hello! I`m agent tester <\/code><\/pre>\n<p>  \u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u0438\u0434\u043d\u043e \u0447\u0442\u043e:  <\/p>\n<ul>\n<li>\u043c\u0435\u0442\u043e\u0434 <b>premain<\/b> \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 <b>main<\/b> \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/li>\n<li>\u0430\u0433\u0435\u043d\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 <code>-javaagent:jarpath[=options]<\/code><\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0430\u0433\u0435\u043d\u0442\u0430 \u043a\u0430\u043a\u0443\u044e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u043b\u044c\u0437\u0443<\/h2>\n<p>  \u0412\u043e\u043e\u0431\u0449\u0435 <b><i>\u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0430\u0433\u0435\u043d\u0442\u043e\u0432 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0431\u0430\u0439\u0442-\u043a\u043e\u0434\u043e\u043c<\/i><\/b>, \u043d\u043e \u0441\u043a\u0430\u0436\u0443 \u0441\u0440\u0430\u0437\u0443 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0439\u0442-\u043a\u043e\u0434 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u043d\u0430\u0447\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0439\u0442\u0438 \u0434\u0430\u043b\u0435\u043a\u043e-\u0434\u0430\u043b\u0435\u043a\u043e \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430. \u041a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 <a href=\"http:\/\/www.csg.ci.i.u-tokyo.ac.jp\/~chiba\/javassist\/\">javassist<\/a> \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0439\u0442-\u043a\u043e\u0434\u043e\u043c \u043d\u0435\u0442.<\/p>\n<p>  \u041d\u0430\u043f\u0438\u0448\u0435\u043c AgentCounter \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0438\u043c\u044f \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441 \u0438 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u043b-\u0432\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u0422\u0430\u043a \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0439 classloader`a.<\/p>\n<pre><code class=\"java\">package ru.habrahabr.agent;  import java.lang.instrument.Instrumentation;  public class AgentCounter { \tpublic static void premain(String agentArgument, Instrumentation instrumentation) { \t\tSystem.out.println(&quot;Agent Counter&quot;); \t\tinstrumentation.addTransformer(new ClassTransformer()); \t} } <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0442\u0435\u043f\u0435\u0440\u044c \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0443 \u043c\u0435\u0442\u043e\u0434\u0430 premain. \u0412 \u043e\u0431\u044a\u0435\u043a\u0442 instrumentation \u044f \u043f\u0435\u0440\u0435\u0434\u0430\u044e ClassTransformer \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0432\u0441\u044e \u0440\u0430\u0431\u043e\u0442\u0443. ClassTransformer \u0431\u0443\u0434\u0435\u0442 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043a\u043b\u0430\u0441\u0441\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 ClassTransformer, \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>java.lang.instrument.ClassFileTransformer<\/code> \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 <code>Instrumentation.addTransformer<\/code><\/p>\n<pre><code class=\"java\">package ru.habrahabr.agent;  import java.lang.instrument.ClassFileTransformer; import java.security.ProtectionDomain;  public class ClassTransformer implements ClassFileTransformer { \t \tprivate static int count = 0; \t \t@Override \tpublic byte[] transform(ClassLoader loader,  \t\t\t\t\t\t\tString className, \t\t\t\t\t\t\tClass&lt;?&gt; classBeingRedefined,  \t\t\t\t\t\t\tProtectionDomain protectionDomain, \t\t\t\t\t\t\tbyte[] classfileBuffer) { \t\tSystem.out.println(&quot;load class: &quot; + className.replaceAll(&quot;\/&quot;, &quot;.&quot;)); \t\tSystem.out.println(String.format(&quot;loaded %s classes&quot;, ++count)); \t\treturn classfileBuffer; \t} } <\/code><\/pre>\n<p>  classfileBuffer \u2014 \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u0431\u0430\u0439\u0442-\u043a\u043e\u0434 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0430\u0439\u0442, \u0434\u043b\u044f \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u0439\u0442, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u043e\u0442 \u0436\u0435 \u043c\u0430\u0441\u0441\u0438\u0432.<\/p>\n<p>  \u041f\u0430\u043a\u0443\u0435\u043c \u0430\u0433\u0435\u043d\u0442 \u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u0432 \u043d\u043e\u0432\u044b\u0439 jar  <\/p>\n<pre><code class=\"bash\">jar -cvfm agentCounter.jar manifest.mf  ru\/habrahabr\/agent\/AgentCounter.class ru\/habrahabr\/agent\/ClassTransformer.class <\/code><\/pre>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0442\u0435\u0441\u0442\u0435\u0440  <\/p>\n<pre><code class=\"java\">package ru.habrahabr.agent;  public class AgentTester { \tpublic static void main(String[] args) { \t\tA a = new A(); \t\tB b = new B();\t\t\t \t\tC c = null; \t} }  class A {}; class B {}; class C {}; <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c AgentTester c \u043d\u043e\u0432\u044b\u043c \u0430\u0433\u0435\u043d\u0442\u043e\u043c  <\/p>\n<pre><code class=\"bash\">java -javaagent:agentCounter.jar ru.habrahabr.agent.AgentTester Agent Counter load class: sun.launcher.LauncherHelper loaded 1 classes load class: ru.habrahabr.agent.AgentTester loaded 2 classes load class: ru.habrahabr.agent.A loaded 3 classes load class: ru.habrahabr.agent.B loaded 4 classes <\/code><\/pre>\n<p><i>\u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u0439 java \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f<\/i><\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u043d\u0438\u0431\u0443\u0434\u044c enterprise \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u0442\u0430\u043a\u0438\u043c \u0430\u0433\u0435\u043d\u0442\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u043f\u043e\u0441\u043b\u0435 \u0441\u0442\u0430\u0440\u0442\u0430 \u0432\u044b\u0434\u0430\u043b \u043c\u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:  <\/p>\n<pre><code class=\"bash\">sun.reflect.GeneratedMethodAccessor230 loaded 33597 classes java.rmi.server.Unreferenced loaded 33598 classes <\/code><\/pre>\n<p>  <\/p>\n<h2>\u0418\u0437\u043c\u0435\u0440\u044f\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 java \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/h2>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0433\u0435\u043d\u0442\u043e\u0432. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 java \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 javaagent \u0431\u0443\u0434\u0435\u0442 \u0438\u0433\u0440\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u0443\u044e \u0440\u043e\u043b\u044c. <b>\u041a\u0442\u043e \u043a\u0430\u043a \u043d\u0438 JVM \u043c\u043e\u0436\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/b>, \u0432 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435 <code>Instrumentation<\/code> \u0435\u0441\u0442\u044c \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>long getObjectSize(Object objectToSize)<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041d\u043e \u043a\u0430\u043a \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u0433\u0435\u043d\u0442\u0443? \u0410 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0438\u0447\u0435\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0433\u043e \u0438 \u043d\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f,<b> javaagent \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 classpath<\/b> \u0438 \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0430\u0433\u0435\u043d\u0442 \u043f\u043e\u043b\u0435 \u0442\u0438\u043f\u0430 Instrumentation instrumentation \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 premain. <\/p>\n<pre><code class=\"java\">package ru.habrahabr.agent;  import java.lang.instrument.Instrumentation;  public class AgentMemoryCounter { \t \tprivate static Instrumentation instrumentation; \t \tpublic static void premain(String args, Instrumentation instrumentation) { \t\tAgentMemoryCounter.instrumentation = instrumentation; \t} \t \tpublic static long getSize(Object obj) { \t\tif (instrumentation == null) { \t\t\tthrow new IllegalStateException(&quot;Agent not initialised&quot;); \t\t} \t\treturn instrumentation.getObjectSize(obj); \t} } <\/code><\/pre>\n<p>  \u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043c\u0435\u0442\u043e\u0434\u0443 <code>AgentMemoryCounter.getSize(obj)<\/code> \u0438\u0437 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.   <\/p>\n<pre><code class=\"java\">package ru.habrahabr.agent;  import java.math.BigDecimal; import java.util.ArrayList; import java.util.Calendar;  public class AgentTester { \tpublic static void main(String[] args) { \t\tprintObjectSize(new Object()); \t\tprintObjectSize(new A()); \t\tprintObjectSize(1); \t\tprintObjectSize(&quot;string&quot;); \t\tprintObjectSize(Calendar.getInstance()); \t\tprintObjectSize(new BigDecimal(&quot;999999999999999.999&quot;)); \t\tprintObjectSize(new ArrayList&lt;String&gt;()); \t\tprintObjectSize(new Integer[100]); \t}  \tpublic static void printObjectSize(Object obj) { \t\tSystem.out.println(String.format(&quot;%s, size=%s&quot;, obj.getClass() \t\t\t\t.getSimpleName(), AgentMemoryCounter.getSize(obj))); \t} }  class A { \tInteger id; \tString name; } <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c  <\/p>\n<pre><code class=\"bash\">java -javaagent:agentMemoryCounter.jar ru.habrahabr.agent.AgentTester Agent Counter Object, size=8 A, size=16 Integer, size=16 String, size=24 GregorianCalendar, size=112 BigDecimal, size=32 ArrayList, size=24 Integer[], size=416 <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 <code>getObjectSize()<\/code> \u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0442.\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u0437\u0430\u0442\u0440\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u043d\u0430 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u043e\u0442 \u043f\u043e\u0441\u0442 \u043f\u043e\u043c\u043e\u0433 \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 javaagent-\u043e\u0432 \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u0442\u043e \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u0441 \u043d\u0438\u043c\u0438 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u043b, \u0442\u0430\u043a\u0436\u0435 \u044f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 javaagent-\u0430 (\u043d\u0435 \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0431\u0430\u0439\u0442-\u043a\u043e\u0434\u0430). \u0410 \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0430\u0433\u0435\u043d\u0442\u044b? \u041d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u0447\u0435\u043d\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e.      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/230239\/\"> http:\/\/habrahabr.ru\/post\/230239\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/0d6\/cea\/505\/0d6cea5059f39fac1c6522285bd73794.png\" align=\"left\"\/><br \/>  \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u043b\u044b\u0448\u0430\u043b\u0438 \u0438\u043b\u0438 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441 \u0442\u0430\u043a\u0438\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c JVM \u043a\u0430\u043a -javaagent, \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"http:\/\/zeroturnaround.com\/software\/jrebel\">Jrebel<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/plumbr.eu\">Plumbr<\/a> \u044d\u0442\u043e \u043c\u043e\u0433\u043b\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a <code>JAVA_OPTS=-javaagent:[path\/to\/]jrebel.jar<\/code> \u0438\u043b\u0438 \u0442\u0430\u043a <code>-javaagent:\/path-to\/plumbr.jar<\/code><br \/>  \u0425\u043e\u0442\u044f <b>javaagent<\/b> \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0435\u0449\u0435 \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 java 1.5, \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0442\u0430\u043a \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0430\u0433\u0435\u043d\u0442\u043e\u0432 \u0438 \u0438\u043c\u0435\u044e\u0442 \u0441\u043c\u0443\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435.<br \/>  \u0427\u0442\u043e \u0436\u0435 \u044d\u0442\u043e \u0437\u0430 \u0430\u0433\u0435\u043d\u0442? \u0417\u0430\u0447\u0435\u043c \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0438 \u043a\u0430\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439?  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-230239","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/230239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=230239"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/230239\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=230239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=230239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=230239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}