Wednesday, 28 October 2015

What is Software architecture?

SOFTWARE ARCHITECTURE

A process of defining a structured solution that meets all of the technical and operational requirements, while optimizing common quality attributes such as performance, security, and manageability.

Software architecture involves :

Series of decisions which can have considerable impact on the quality, performance, maintainability, and overall success of the application.

Ø  Functionality
Ø  Usability
Ø  Resilience
Ø  Performance
Ø  Reuse
Ø  Comprehensibility
Ø  Economic
Ø  Technology constraints
Ø  Tradeoffs

How substring memory leak fixed in JDK 1.7?

Resolve the memory leak in JDK 1.7

substring() method implementation in JDK 1.6

Substring and original string represent the same Character array which leads to memory leak.


    
public String substring(int beginIndex, int endIndex) {
     //check boundary
return ((beginIndex == 0) && (endIndex == count))?this :
  new String(offset + beginIndex, endIndex - beginIndex, value);
    }

    String(int offset, int count, char value[]) {
this.value = value;
     this.offset = offset;
     this.count = count;
    }

substring() method implementation in JDK 1.7
This problem fixed in the JDK 1.7 by returning the new copy of character array.

    public String(char value[], int offset, int count) {
    //check boundary
     // It return new copy on array.
    this.value = Arrays.copyOfRange(value, offset, offset + count);
    }

    public String substring(int beginIndex, int endIndex) {
    //check boundary
    int subLen = endIndex - beginIndex;
    return new String(value, beginIndex, subLen);
    }

How to resolve the memory leak problem of substring() method in JDK 1.6?

Resolve the memory leak in JDK 1.6

subString = string.substring(3, 10) + "";

In above code, the string.substring(3, 10) will return the substring which point to original string array and the substring will not allow the garbage collection for old string (char value[]).

But when we add the empty string to offset, new string will form in constant pool with new char value[] array and we can overcome the problem of garbage collection of old string array.

Memory leak in JDK 1.6 substring method

How does substring() method works in JDK 1.6

String in java is a sequence of characters (maintained as char array value[]).

String is more like a utility class which works on that character sequence.

private final char value[];

/** The offset is the first index of the storage that is used. */
private final int offset;
/** The count is the number of characters in the String. */
private final int count;

These two private variables (offset and count) used to manage the char array.

When we create a substring of String using substring() method, this method assigns the new values of offset and count variables every time.

Problem with above approach till JDK 1.6 (Memory leak)

If you have a VERY long string, but we only need a small part each time by using substring(). substring() method will return the offset and count which refers the original string array which will not permit to garbage collection of original string array.

This will cause a performance problem, since we need only a small part and keeping the whole char value[] array (No garbage collection).

    public String substring(int beginIndex, int endIndex) {
     //check boundary
return ((beginIndex == 0) && (endIndex == count))?this :
  new String(offset + beginIndex, endIndex - beginIndex, value);
    }

    String(int offset, int count, char value[]) {
this.value = value;
     this.offset = offset;
     this.count = count;
    }

Resolve the memory leak in JDK 1.6

subString = string.substring(3, 10) + "";

In above code, the string.substring(3, 10) will return the substring which point to original string array and the substring will not allow the garbage collection for old string (char value[]).

But when we add the empty string to offset, new string will form in constant pool with new char value[] array and we can overcome the problem of garbage collection of old string array.

Tuesday, 27 October 2015

Java ArrayList trimToSize() Method

Java ArrayList trimToSize() Method

trimToSize() method is used for memory optimization. It trims the capacity of ArrayList to the current list size.

public void trimToSize()

Given arraylist is having capacity of 10 but there are only 3 elements in it, calling trimToSize() method on this ArrayList would change the capacity from 10 to 3.



class TrimToSizeExample {

     public static void main(String args[]) {
           ArrayList<Integer> arraylist = new ArrayList<Integer>(10);
           arraylist.add(1);
           arraylist.add(2);
           arraylist.add(3);
                        // debug and find that the capacity of array list is 10
             // [1, 2, 3, null, null, null, null, null, null, null]
           System.out.println(arraylist.size());

           arraylist.trimToSize();

                        // debug and find that the capacity of array list is 3
             // [1, 2, 3]
           System.out.println(arraylist.size());
     }
}
Output:
3
3

Monday, 26 October 2015

What are disadvantages of deep cloning using Serialization?

Disadvantages of using Serialization to achieve deep cloning

1. Serialization is more expensive than using object.clone().

2. Not all objects are serializable.

3. Serialization is not simple to implement for deep cloned object.

Can be declare enum inside the interface?

It's perfectly legal to have an enum declared inside an interface.

In this situation the interface is just used as a namespace for the enum and nothing more. The interface is used normally wherever we use it.


public interface IService {
     public enum Status { // enum
           OK(200), INTERNAL_ERROR(500), KO(0);

           private int errorCode;

           private Status(int errorCode) {
                this.errorCode = errorCode;
           }

           public int getErrorCode(){
                return errorCode;
           }
     }
}

How to compile Java program version specific?

Compile program Java version specific

To compile program in a specific Java version, use -source option

Example
javac -source 1.3 Demo.java

javac -source 1.4 Demo.java

What is JNI and its advantages and disadvantages?

JNI (Java Native Interface)

JNI is used to call functions written in other languages than Java.

Advantages

1. Use existing libraries which was written in other language.
2. Call Windows API function.
3. Maintains execution speed.
4. Call API function of some server product which is in C or C++ from Java client.

Disadvantage

1. Can't say write once run anywhere.
2. Difficult to debug runtime error in native code.
3. Potential security risk.
4. Can't call it from an Applet.

How to sort list of case insensitive strings?

Sort list of strings - case insensitive

To sort list of strings ignoring the case

Using java library flag


Collections.sort(list, String.CASE_INSENSITIVE_ORDER);


By passing own comparator


Collections.sort(list, new Comparator<String>() {
     @Override
     public int compare(String s1, String s2) {
           return s1.compareToIgnoreCase(s2);
     }
});

Related Posts Plugin for WordPress, Blogger...