What is premain() and how does it get called?
The premain
is a mechanism associated with the java.lang.instrument
package, used for loading "Agents" which make byte-code changes in Java programs.
The mechanism is explained in the java.lang.instrument
documentation.
The gist of it is that the "agent" is deployed in a jar, and that jar has a special entry in its manifest, that tells the instrumentation package where to look for the premain method. The source you quoted is supposed to be a simple agent.
Minimal runnable example
GitHub upstream: https://github.com/cirosantilli/java-cheat/tree/d73d2786cad458973a6b46bc98b9faabae65f3e1/instrument
META-INF/MANIFEST.MF
Premain-Class: Sizeof
Sizeof.java
import java.lang.instrument.Instrumentation;
final public class Sizeof {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long sizeof(Object o) {
return instrumentation.getObjectSize(o);
}
}
Main.java
final public class Main {
public static void main(String [] args) {
System.out.println("Object");
System.out.println(Sizeof.sizeof(new Object()));
System.out.println("/\"\"");
System.out.println(Sizeof.sizeof(""));
System.out.println("/\"abc\"");
System.out.println(Sizeof.sizeof("abc"));
System.out.println("int[0]");
System.out.println(Sizeof.sizeof(new int[0]));
System.out.println("int[10]");
System.out.println(Sizeof.sizeof(new int[10]));
class OneInt {
public int i;
}
System.out.println("OneInt");
System.out.println(Sizeof.sizeof(new OneInt()));
class TwoInts {
public int i;
public int j;
}
System.out.println("TwoInts");
System.out.println(Sizeof.sizeof(new TwoInts()));
class IntArray0 {
int[] i = new int[0];
}
System.out.println("IntArray0");
System.out.println(Sizeof.sizeof(new IntArray0()));
class IntArray10 {
int[] i = new int[10];
}
System.out.println("IntArray10");
System.out.println(Sizeof.sizeof(new IntArray10()));
}
}
Makefile
all:
javac *.java
jar -cfm Sizeof.jar META-INF/MANIFEST.MF Sizeof.class
java -ea -javaagent:Sizeof.jar Main
Sample output:
Object
16
/""
24
/"abc"
24
int[0]
16
int[10]
56
OneInt
16
TwoInts
24
IntArray0
16
IntArray10
16
Tested in Ubuntu 16.10, Java HotSpot 1.8.0_92.