Sunday, January 31, 2010

Java, J2EE Performance Tuning Interview Questions

Question 1:Assume a multiplication operation in java takes 10 milliseconds. So, what is the running time of the following for() loop?
int k = 10;
for(int i=0; i<100; k =" 20">



Answer:-The for loop iterates only once, not 100 times. So the running time is 10 millisecond. Because, the loop evaluates to a constant result ( that is, k = 20 * 100 will yield same result for 100 times) . So, jvm is smart enough, runs the for() loop once and saves time. I just mention this, because the developer should not worry about expression level optimization, which is taken care by the jvm itself.

Question 2: You have to write a java program that can read files of varying sizes, ranging from 100 KB (very small) to few GBs (large size).


Answer:-You can write an intelligent program in this case :). If the file size is smaller, then read the whole file content in one-go. Read an example program given here. But, what if the file size is big? In this case, the program can’t read the whole file in one shot, as it would run out of memory. So, you can write the program in such a way that it reads the file content in byte arrays for several iterations. In this case, the running time of the program will depend on the size of byte array. So, how will you smartly determine the size of byte array?You can use Runtime.getRuntime().freeMemory() to find the freely available heap size. Based on this available memory, your program can create smaller or larger byte arrays at runtime. The larger the byte arrays, the lesser the response time.

Question 3:You have to use JDBC (type 4 driver) to retrieve some values from two different tables in database. As you have to use join operation in this case, will you consider using join in the sql query level or in the program level?


Answer:-You have to move the join operation to query level. Never try to do (joining, sorting, aggregating, grouping and etc) such things application level, unless otherwise it is required. Because, SQL processor may optimize the join and prepare the better execution plan than you!

Question 4:How will you plan your optimization strategy for a given J2EE (or Java) application?


Answer:-
1. Use profiler tools (there are so many.. example is JProfiler), monitoring tools and generate reports. From the report, analyze the response time of each component.
2. First, you have to analyze the response time of read/write tasks such as: I/O, Database access. Because, that is where most of the time spent.
3. Look for poor code that doesn’t release database connection or doesn’t close files and etc.
4. Minimize the repeated interaction with components as much as possible.

Question 5: Give some strategy to optimize web tier (JSP/Servlets)?


Answer:-
1. Clearly identify static and dynamic portion of the web components. Enable client browser-level-cache for static web resource to save the network roundtrip.
2. Don’t dump all the data in session object, because that may lead your server running out of space at sometime.
3. Enable for all sticky servlet (servlets that are accessed many times).
4. Analyze whether you can zip and transfer some of the content.

Question 6: How will you tune the Data base tier?


1 comment:

  1. Hi Neeraj,

    You make learning and reading addictive. All eyes fixed on you. Thank you being such a good and trust worthy guide.

    Hello people, So I was wanting some help returning a variable to a page in tymeleaf. If I could get help just converting one of my methods it would open the door for me and I'd greatly appreciate it. I tried reading the thymeleaf docs but I am having trouble with it.

    Once again thanks for your tutorial.

    Shukran,
    Tarun

    ReplyDelete