Goldman Sachs Interview Question for Java Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 5 vote

stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object

- subahjit July 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

posting the contents here:

You can use the java.lang.instrument package

Compile and put this class in a JAR:

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);
    }

}
Add the following to your MANIFEST.MF:

Premain-Class: ObjectSizeFetcher
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

- algorithmor November 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hi,

You can use getObjectSize(Object o) from java.lang.Instrument.Instrumentation Interface.
Although, it will not be a good idea to code your business logic according to how much memory they are taking as Memory is handled by JVM itself.

- Ajay Sharma July 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive procedure to calculate each size of member variables

- Debasis Jana July 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's a question of Java Reflection

- Debasis Jana July 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The intent of the interviewer is certainly Reflection, but you can serialize the object and calculate the number of bytes stored, as well. The same approach should work for different objects and give a relative "size" which can then be compared.

The writeObject method should be overridden such that only the member variables of the object are serialized. (Since we are talking about containers). Then a bytecount would return reliable comparison results. This should work since all objects are custom serialized the same way,

Just food for thought.

- SoMiE July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maybe you should use sun.misc.Unsafe class?

- gstepanov@griddynamics.com September 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about looking for free heap memory using Runtime class before and after allocating the object

- Harsha September 27, 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