Adobe Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
The boolean mapping was done with a 32bit CPU in mind. The int value has 32 bits so it can be processed in one operation.
Here's a solution from Peter Norvig's Java IAQ: Infrequently Answered Questions (norvig.com/java-iaq.html#sizeof) to measure the size (with some imprecision):
static Runtime runtime = Runtime.getRuntime();
...
long start, end;
Object obj;
runtime.gc();
start = runtime.freememory();
obj = new Object(); // Or whatever you want to look at
end = runtime.freememory();
System.out.println("That took " + (start-end) + " bytes.");
You can use the java.lang.instrumentation package:
- jayram singh May 30, 2013docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html
It has a method that can be used to get the implementation specific approximation of object size, as well as overhead associated with the object.
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
Use getObjectSize:
public class C {
private int x;
private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}
Invoke with:
java -javaagent:ObjectSizeFetcherAgent.jar C