Oracle Interview Question for Web Developers






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

ByteArrayOutputStream bao = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bao);
oos.writeObject(obj);
oos.flush();
int size = bao.size();
oos.close();

- Anonymous August 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not sure but this code only for serializable objects right?

- Anonymous November 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maybe this:

long l1 = Runtime.freeMemory();
obj.clone();
long l2 = Runtime.freeMemory();
System.out.println((l1-l2)+" bytes");

Although the javadoc for Runtime.freeMemory() says that this method returns only an approximate value.

- Anonymous December 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

(Correcting my earlier post)
Better to do the cloning in a loop because if the obj size is just few bytes like 5-6 bytes then the freeMemory() might give same result for l1 and l2 as its an approximation.

So:
long l1 = Runtime.freeMemory();
n=10;
for(int i=1;i<=n;i++){
obj.clone();
}
long l2 = Runtime.freeMemory();
System.out.println(((l1-l2)/n)+" bytes");

- Anonymous December 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the clone is not a deep clone...If the object contains another user-defined class, does this work?

- young April 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In case, data members are (references of )objects: We will have to overRide clone() to clone the data members and ensure that data members are cloned fully/correctly.

- Monish October 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

somewhere i read.. jvm can increase the help size if required. So be careful. It same to use Runtime.totalMemory()- Runtime.freeMemory() to get usedMemory. Runtime.GC() before doing all these. and wait for some more time since GC() is not blocking call.

- Master December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess this can be done with a bit of reflection.
Assumption - in Java object size = size of the instance level fields
and in Java each type has a predefined size.

- dhrubo August 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume 32 bit for references

- dhrubo August 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be solved by bit option.

{{public class PrintSize{
public static void main(String[] argv){
int x = ~0;
int i=0;
while (0 != x){
//System.out.println("x: " + x);
x >>>=1;i++;
}
System.out.println("int size is "+i);
}
}
}}

Output: int size is 32

- Song August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I forgot that it is non-primitive.

- Song August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I forgot that it is non-primitive.

- Song August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I forgot that it is non-primitive.

- Song August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I forgot that it is non-primitive.

- Song August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I forgot that it is non-primitive.

- Song August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I forgot that it is non-primitive.

- Song August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Still useful

- Piyush December 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Java instrumentation one can use getObjectSize(object)
public static void premain(String agentArgs, Instrumentation inst)

- kaks August 01, 2011 | 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