EzDevInfo.com

jvm interview questions

Top jvm frequently asked interview questions

How do I set the proxy to be used by the JVM

Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.

I am behind a proxy server. How do I set my JVM to use the proxy ?


Source: (StackOverflow)

How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?

How can I tell if the JVM my application runs in is 32 bit or 64-bit? Specifically, what function or preference do I access to detect this within the program?


Source: (StackOverflow)

Advertisements

What are the Xms and Xmx parameters when starting JVMs?

Please explain the use of Xms and Xmx parameters in JVMs. What are the default values for them?


Source: (StackOverflow)

Real differences between "java -server" and "java -client"?

Is there any real practical difference between "java -server" and "java -client"? All I can find on Sun's site is a vague "-server starts slower but should run faster". What are the real differences? (Using JDK 1.6.0_07 currently.)


Source: (StackOverflow)

What is the difference between JVM, JDK, JRE & OpenJDK?

What is the difference between JVM, JDK, JRE & OpenJDK?

I was programming in Java and I encountered these phrases, what are the differences between them?


Source: (StackOverflow)

In which language are the Java compiler, JVM and Java written?

In which languages are the Java compiler (javac), the virtual machine (JVM) and the java starter written?


Source: (StackOverflow)

Java "Virtual Machine" vs. Python "Interpreter" parlance?

It's seems rare to read of a Python "virtual machine" while in Java "virtual machine" is used all the time. Both interpret byte codes, why call one a virtual machine and the other an interpreter?


Source: (StackOverflow)

Does the JVM prevent tail call optimizations?

I saw this quote on the question: What is a good functional language on which to build a web service?

Scala in particular doesn't support tail-call elimination except in self-recursive functions, which limits the kinds of composition you can do (this is a fundamental limitation of the JVM).

Is this true? If so, what is it about the JVM that creates this fundamental limitation?


Source: (StackOverflow)

How to set Oracle's Java as the default Java in Ubuntu?

How do I change the value of JAVA_HOME in Ubuntu to point to Oracle's Java?

Should it point to java-6-sun or java-6-sun-1.6.0.24 ?


Source: (StackOverflow)

What does -XX:MaxPermSize do?

Specifically, why would it help to fix a PermGen OutOfMemoryError issue?

Also, bonus points for an answer that points me to the documentation on JVM arguments...


Source: (StackOverflow)

Could not reserve enough space for object heap

When I am running I am getting the following exception repeatedly each time I try to run the program.

Error occurred during initialization of VM

Could not reserve enough space for object heap

Could not create the Java virtual machine.

I tried to increase my virtual memory (page size) and RAM size, but to no avail. Can anyone tell me how can I eliminate this error?


Source: (StackOverflow)

How do I write a correct micro-benchmark in Java?

How do you write (and run) a correct micro-benchmark in Java?

I'm looking here for code samples and comments illustrating various things to think about.

Example: Should the benchmark measure time/iteration or iterations/time, and why?

Related: Is stopwatch benchmarking acceptable?


Source: (StackOverflow)

Why does this method print 4?

I was wondering what happens when you try to catch an StackOverflowError and came up with the following method:

class RandomNumberGenerator {

    static int cnt = 0;

    public static void main(String[] args) {
        try {
            main(args);
        } catch (StackOverflowError ignore) {
            System.out.println(cnt++);
        }
    }
}

Now my question:

Why does this method print '4'?

I thought maybe it was because System.out.println() needs 3 segments on the call stack, but I don't know where the number 3 comes from. When you look at the source code (and bytecode) of System.out.println(), it normally would lead to far more method invocations than 3 (so 3 segments on the call stack would not be sufficient). If it's because of optimizations the Hotspot VM applies (method inlining), I wonder if the result would be different on another VM.

Edit:

As the output seems to be highly JVM specific, I get the result 4 using
Java(TM) SE Runtime Environment (build 1.6.0_41-b02)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)


Explanation why I think this question is different from Understanding java stack:

My question is not about why there is a cnt > 0 (obviously because System.out.println() requires stack size and throws another StackOverflowError before something gets printed), but why it has the particular value of 4, respectively 0,3,8,55 or something else on other systems.


Source: (StackOverflow)

Maximum number of parameters in Java method declaration

What is the maximum number of parameters that a method in Java can have and why?

I am using Java 1.8 on a 64-bit Windows system.

All the answers on StackOverflow about this say that the technical limit is 255 parameters without specifying why.

To be precise, 255 for static and 254 for non-static (this will be the 255th in this case) methods.

I thought this could be specified in some sort of specification and that this was simply a statically defined maximum number of parameters allowed. But this was only valid for int and all 4-bytes types. I did some tests with long parameters, and I was only able to declare 127 parameters in that case.

With String parameters, the allowed number i deduced from testing is 255 (it may be because the reference size is 4 bytes in Java?).

But since I am using a 64-bit system, references size should be 8 bytes wide and so with String parameters the maximum allowed number should be 127, similar to long types.

How does this limit is exactly applied?

Does the limit have anything to do with the stack size of the method?

Note: I am not really going to use these many parameters in any method, but this question is only to clarify the exact behavior.


Source: (StackOverflow)

Java: int array initializes with nonzero elements

According to the JLS, an int array should be filled by zeros just after initialization. However, I am faced with a situation where it is not. Such a behavior occurs first in JDK 7u4 and also occurs in all later updates (I use 64-bit implementation). The following code throws exception:

public static void main(String[] args) {
        int[] a;
        int n = 0;
        for (int i = 0; i < 100000000; ++i) {
            a = new int[10];
            for (int f : a)
                if (f != 0)
                  throw new RuntimeException("Array just after allocation: "+ Arrays.toString(a));
            Arrays.fill(a, 0);
            for (int j = 0; j < a.length; ++j)
                a[j] = (n - j)*i;
            for (int f : a)
                n += f;
        }
        System.out.println(n);
    }

The exception occurs after the JVM performs compilation of the code block and does not arise with -Xint flag. Additionally, the Arrays.fill(...) statement (as all other statements in this code) is necessary, and the exception does not occurs if it is absent. It is clear that this possible bug is bounded with some JVM optimization. Any ideas for the reason of such a behavior?

Update:
I see this behavior on HotSpot 64-bit server VM, Java version from 1.7.0_04 to 1.7.0_10 on Gentoo Linux, Debian Linux (both kernel 3.0 version) and MacOS Lion. This error can always be reproduced with the code above. I did not test this problem with a 32-bit JDK or on Windows. I already sent a bug report to the Oracle (bug id 7196857) and it will appear in public Oracle bug database in few days.

Update:
Oracle published this bug at their public bug database: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7196857


Source: (StackOverflow)