Adobe Interview Question for Applications Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 2 vote

You can use the java.lang.instrumentation package:

docs.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

- jayram singh May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this code,i already tried,but not working....

- Anonymous May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes, I also faced the same problem

- sagar May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can try this.

stackoverflow.com/questions/9368764/calculate-size-of-object-in-java

- sjain May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.");

- Psycho June 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we want to measure the size of an object in multi-threading environment, then how?

- Anonymous July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its simple. We will have to measure it.

- Manasvi Goyal June 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we should serialize the object and get the bytes required to store it.

- test September 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Sorry, Now I got the question.

We should search for its size on Google search.

- Manasvi Goyal June 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Sorry, Now I got the question.

We should search for its size on Google search.

- Manasvi Goyal June 19, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More