Saturday, May 3, 2014

What are the 3 Goals of Software Development ?

3 Primary Goals of Software Development-

During Software development of new project or extending existing project , 3 primary goals to be considered are -to make the software faster, better, cheaper.
These 3 goals viz faster, better, cheaper  correspond to Time, Features and Money respectively.

Faster - The software has to meet a market window. Competitive organizations set that time
Better - The software has to serve the requirements of the process it supports.
Cheaper - The software has to be less expensive to produce and maintain

Interestingly, All three of these goals, compete against each other.

Understanding how these three goals compete against each other -
The three goals create a negative perpetual cycle. More features means the project takes more time. More time means the project costs more money. More money means that the client wants more features. The cycle has now come full circle and repeats itself.

How to break the negative perpetual cycle  due to Faster,Better, Cheaper goals-

1) Scoping the project 

First Simple Solution to the above problem is Scoping the project. The project can be delivered on time and with lower cost by limiting the entire set of features to three main features.Now this might seem like a scary idea at first but consider this-Most software projects throw every idea possible into the mix because "it must have this feature". Of course this results in a huge feature set and the project is doomed before it has even begun. A wiser way to determine the features required is to only pick the ones that will help achieve the primary purpose of the software. This should be no more than three main features.
Benefits of Scoping the project or lowering expectations (by limiting feature set) -
By reducing your feature set and shipping on time you won't have wasted years developing a product then finding out you got it wrong or all those "must have features" are not even used. This will save you money, time and your sanity.You will also know exactly what you must do next for version 2 of your product. Again you pick the 2-3 most important features and ship them. Then you will receive more information and a positive feedback cycle is starting to occur.

2) Software Reuse

Software reuse can cut software development time and costs.
Software reuse is defined as the process of creating software systems from predefined software components.
Software specifications, designs, tests cases, data, prototypes, plans, documentation, frameworks, and templates are all candidates for reuse.

The major advantages for software reuse are to:

  1. Increase software productivity.
  2. Shorten software development time.
  3. Improve software system interoperability.
  4. Develop software with fewer people.
  5. Move personnel more easily from project to project.
  6. Reduce software development and maintenance costs.
  7. Produce more standardized software.
  8. Produce better quality software and provide a powerful competitive advantage.

Friday, October 25, 2013

Windows 7 - How to always permanently run application as administrator ?

If you do not want to have to continually choose run as administrator every time you want to use an application (say eclipse) then can set this option as permanent.
You must follow these steps to make this work. 
  1. Go to the directory where executable which runs the application is located.
  2. Right click on the executable which is used  to launch the application. e.g.  eclipse.exe
  3. Choose Properties from the menu.
  4. Go to the Compatibility tab.
  5. In the Privilege Level check the check box indicating Run this program as an administrator
          

Tuesday, August 20, 2013

What is 'java.lang.OutOfMemoryError: GC overhead limit exceeded'

The Garbage Collector in JVM throws this exception when too much time is spent in garbage collection for too little return. e.g. 98% of CPU time is spent on GC and less than 2% of heap is recovered.


This effectively means that your program stops doing any progress and is busy running only the garbage collection at all time.
To prevent your application from consuming up CPU time without getting anything done, the JVM throws thisError so that you have a chance of diagnosing the problem.

Solution
  1. Increase the heap size using -Xmx option, for example -Xmx1g.
  2. Enable the concurrent low pause collector -XX:+UseConcMarkSweepGC.
  3. Reuse existing objects when possible to save some memory.
If necessary, the limit check can be disabled by adding the option -XX:-UseGCOverheadLimit to the command line although this is not advised.
For more details please check this article

Wednesday, February 20, 2013

"Not enough storage is available to process this command" while using jmap or jstack


Issue :
Sometimes while using a tools like jmap,jstack on a running java process, we are not able to connect to the java process and below error is observed.

Not enough storage is available to process this command 
[Note : Here PID  is the process id of java process whose dump is to be captured.]


Error Message

Solution :

Scenario 1)
If the service is running as a specific user, you need connect by logging into the same session and then try to run jmap or jstack etc.

Scenario 2)
If you are using Remote Desktop, you need to connect using "mstsc /admin" (used to be /console) and try to run jmap or jstack again

Scenario 3) 
Target Java process is running as System Process (in case of Windows)
In case if the target java process is running as System Process, we need to use PsExec utility.
Steps to use PsExec utility :
1) Download the PsTools from here.
2) Unzip the PsTools and copy the PsExec.exe
3) Execute the original command using PsExec utility with -s
    Syntax :
  PsExec -s {original_command}

    Example :
    PsExec -s jmap -dump:format=b,file=c:/heapdump.bin 4708    
Example - Executing  jmap using psexec.

Friday, October 14, 2011

HP-UX -How to find out the Processor Architecture( RISC or IA)

If you want to make out whether the HP-UX OS is running on PA(i.e. PA-RISC) or IA h/w without bothering about the specifics of the model of the processor, you can find that from the kernel itself :
file /stand/vmunix

e.g. :

bash-3.2# file /stand/vmunix

/stand/vmunix: ELF-64 executable object file - PA-RISC 2.0 (LP64)

Note:
Recent versions support the HP 9000 series of computer systems, based on the PA-RISC processor architecture, and HP Integrity systems, based on Intel's Itanium architecture

Saturday, April 2, 2011

Elephant Thinking


My grandmother had a green parrot.
This parrot used to speak like humans whatever was taught to him.
My grandmother had taught him to speak words like vithal,vithal.
Humans have this unique mechanism of sound box
and vocal chords which allows them to speak.Every time a person speaks, air enters and makes the vocal
chords to vibrate which produces a sound and this gives the ability to speak.
Now if someone would have told this parrot "Humans can speak because they have this inbuilt mechanism,
and there is no such mechanism in parrots and hence the parrot cannot speak", the parrot would have not been
able to speak.
The very thought to immitate human voice was the motivating force for the parrot.
Simmilarly an elephant when it is young is tied to a tree with a very strong chain.
The elephant tries lot to break the chain but is not successful and finally gives up.
When the elephant grows old and is tied to the tree with a chain, the elephant does not attempt to break
the chain since it knows that when it was small, it was not able to break the chain.But the elephant does not realize
that the chain which seemes to be really strong when it was young is not as much strong now when it has grown old
and can be easily broken. This is known as elephant thinking.The elephant is constrained by preconcived notions that
it had developed when it was young.
Conditions do differ with space and time.System undergoes changes with space and time.

Friday, October 22, 2010

How to generate a threaddump for java based process

Windows
Can be captured in following ways

1) Using Ctrl-Break :
The Java application that you want to produce a thread dump for must be running / started in a command console. When you want to produce a thread dump press Ctrl-Break.

2) Using jstack :
step 1) Use jps command to find out the process id (pid) of the java process for which the thread dump is to be generated.
eg output for jps
C:\>jps
3316 Deadlock
3476 Jps
step 2) Use jstack command to along with the process id to generate the threaddump.
syntax : jstack
eg: c:/>jstack 3316

Linux or Solaris

1)Using Ctrl-\:
If the JVM is running in a console then simply press Ctrl-\.

2)Sending QUIT signal:
If the JVM is running in the background then send it the QUIT signal:

kill -QUIT process_id

There process_id is the process number of the running Java process. The thread dump will be sent to wherever standard out is redirected too.

You can generally get the process numbers of of all running Java processes with the command:

ps axf | grep java

3) Using jstack:
Steps as mentioned above in the Windows Section

Why Thread Dump is required ?

You need to generate threaddump of java based process whenever...
1) JVM refuses to exit cleanly.This could be due to
           a) Some user defined thread running infintely
           b) Some threads getting deadlock
           c) Some threads going in infinite wait state.
2) Application is unresponsive.
3) Application is not behaving as expected.

Tuesday, May 11, 2010

Robert Frost: The Road Not Taken (1915)


Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth.

Then took the other, as just as fair,
And having perhaps the better claim,
Because it was grassy and wanted wear;
Though as for that the passing there
Had worn them really about the same.

And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way,
I doubted if I should ever come back.

I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I--
I took the one less traveled by,
And that has made all the difference.

Monday, April 26, 2010

Hindi translation of Robert Frost's "Woods are lovely dark and deep..." by Harivansh Rai Bachchan


“ The woods are lovely, dark and deep

But I have promises to keep,

and miles to go before I sleep. ”

- Robert Frost


Stopping By Woods on a Snowy Evening

Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.

My little horse must think it queer
To stop without a farmhouse near
Between the woods and frozen lake
The darkest evening of the year.

He gives his harness bells a shake
To ask if there is some mistake.
The only other sound's the sweep
Of easy wind and downy flake.

The woods are lovely, dark and deep.
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.

“Stopping by Woods on a Snowy Evening” is regarded as Frost's masterpiece.

The speaker, while traveling on horseback (or in a horse-drawn sleigh) on the darkest evening of the year, stops to watch the woods fill up with snow. He thinks the owner of these woods is someone who lives in the village and will not see the speaker stopping on his property. While the speaker continues to gaze into the snowy woods, his little horse impatiently shakes the bells of its harness. The speaker describes the beauty and allure of the woods as “lovely, dark, and deep,” but reminds himself that he must not remain there, for he has “promises to keep,” and a long journey ahead of him.

Below is the the hindi translation of the last stanza of the original master piece by Harivansh Rai Bachchan

गहन सघन मनमोहक वन तरु मुझको आज बुलाते हैं,
किन्तु किये जो वादे मैंने याद मुझे वोह आते हैं,
अभी कहाँ आराम बड़ा यह मूक निमंत्रण छलना हैं,
अरे अभी तो मीलों मुझको , मीलों मुझको चलना




Monday, April 5, 2010

Alternative to 'top' command on Solaris

prstat Command:
prstat utility iteratively examines all active processes on the system and reports statistics based on the selected output mode and sort order. The prstat command provides output similar to the ps command.

Options for prstat Command

Option

Description

How It Can Help

No option

Displays a sorted list of the top processes that are consuming the most CPU resources. List is limited to the height of the terminal window and the total number of processes. Output is automatically updated every five seconds. Ctrl-C aborts.

Output identifies process ID, user ID, memory used, state, CPU consumption, and command name.

-n number

Limits output to number of lines.

Limits amount of data displayed and identifies primary resource consumers.

-s key

Permits sorting list by key parameter.

Useful keys are cpu (default), time, and size.

-v

Verbose mode.

Displays additional parameters.


eg: To find the top 5 process consuming CPU in Solaris
prstat -n 5 -s cpu


Ref: http://docs.sun.com/source/819-1892-12/FMASolarisCmd.html#22021

Sunday, March 21, 2010

Good Books on Design Patterns

There are different ways in which people appreciate design patterns.
Some do it in extremely theoretical ways while some do it in extremely practical ways.
Some experience an "Aha!" moment when they finally see the great power of the patterns - when they finally realise the exact design patterns which would help them with the task at their work.
Design Patterns help us to welcome change rather than fighting with it.
Finding the right book on the subject is very important.
Below are the four books that I would recommend.
  1. Design Patterns -Elements of Reusable Object-Oriented Software - GoF -Pearson Education Publication
  2. Head First Design Patterns- Elisabeth Freeman, Eric Freeman, Kathy Sierra and Bert Bates -O'relliy Publication
  3. Holub on Patterns-Learing Patterns by Looking at Code -Allen Holub- Apress Publication
  4. Java Design Patterns-James Cooper-Pearson Education Publication

Why Vector and Hashtable violate the naming convention specified by Java Collection Framework?

Vector and Hashtable were introduced in JDK 1.0 .
The Java Collections Framework was introduced with JDK 1.2.
Due to backward compatiblity issues the names of Vector and Hashtable could not be changed.

Therefore the "table" portion of Hashtable could not have the "t" changed to uppercase "T." In fact, there is a naming convention for implementations and interfaces in the Java Collection Framework and neither Hashtable nor Vector could be changed to meet this convention. Most implementations in the Java Collections Framework (such as ArrayList, HashMap, and TreeSet) have their name formed from an implementation detail as the first portion of the name and having the name end with the interface implemented.

However the Vector and Hashtable classes were retrofitted to be part of this new Java Collections Framework and, as part of this, to implement interfaces from that framework. Vector was altered to implement List . Simmilarly Hashtable was altered to implement Map

Monday, January 25, 2010

SocketException in Java

In this post we will look into the details of 2 kinds of SocketException in Java
1) java.net.SocketException: Connection reset and
2) java.net.SocketException: Connection reset by peer: socket write error

1) java.net.SocketException: Connection reset

Occurs when :
While reading from the socket(inputstream)

Occurs where:
Server side or Client side

Reason:
If program(server/client) tries to read data from (client/server) using sockets InputStream and the other side(client/server) has gone down unexpectedly i.e crashed without closing its sockets or OutputStream.

However:
Note if the other(client/server) side closes its socket or related OutputStream (belonging to socket),this exception does not occur at the program(server/client) side.Closing the socket closes all the related output and input streams.

Summary:
If program tries to read from a socket input stream and the other side has gone down(crashed),or thread completes its run without closing the outputstream or socket.

Stack Trace:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.net.SocketInputStream.read(SocketInputStream.java:182)
at java.io.FilterInputStream.read(FilterInputStream.java:66)
at SocketServerDemo.main(SocketServerDemo.java:27)
---------------------------------------------------------------------------------
2) java.net.SocketException: Connection reset by peer: socket write error

Occurs when :
While writing to the socket(outputstream)

Occurs where:
Server side or Client side

Reason:
If program(server/client) tries to write data to (client/server) using sockets OutputStream and the other side(client/server) has gone down unexpectedly i.e crashed without closing its sockets or InutputStream.

However:
Note if the other(client/server) side closes its socket or related InputStream (belonging to socket), this exception does not occur at the program(server/client) side.Closing the socket in turn closes all the related output and input streams.

Summary:
If program tries to write to the socket's output stream and the other side has gone down(crashed),or thread completes its run without closing the inputstream or socket.

Stack Trace:
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
at java.io.DataOutputStream.write(DataOutputStream.java:71)
at ConnectionResetServerDemo_1.main(ConnectionResetServerDemo_1.java:15)
---------------------------------------------------------------------------------

How to convert Exception Stack Trace to String in Java ?

Below code snippet shows how to convert a Exception Stack Trace to String object in Java

public static String convertExStackTraceToString(Exception ex)

{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
sw.flush();
String strStackTrace = sw.toString();
try
{
sw.close();
pw.close();
}catch (IOException ex1)
{
ex1.printStackTrace();
}
return strStackTrace;
}

Saturday, January 23, 2010

How "thread safe" is your class ?

This post discusses the various thread safety levels for a java classes.
Thread safety of a class is not an
all-or-nothing proposition and is difficult to define.
eg: The methods of Vector are all synchronized, and Vector is clearly designed to function in multithreaded environments. But there are limitations to its thread safety, namely that there are state dependencies between certain pairs of methods. (Similarly, the iterators returned by Vector.iterator() will throw a ConcurrentModificationException if the Vector is modified by another thread during iteration.)

Joshua Bloch has suggested following five levels of thread safety of a class :

Immutable: Instances of the class are constant and cannot be changed. There are, therefore, no thread safety issues.
eg:Integer,String,and BigInteger

Thread-safe: Instances of the class are mutable but they can be used safely in a concurrent environment. All methods provided by the class are properly synchronized either at the interface level or internally within the method.
eg:Timer

Conditionally thread-safe: Instances of the class for which each individual operation may be thread-safe, but certain sequences of operations may require external synchronization.
eg:Hashtable and Vector
Explanation-Iterator returned by Vector class assumes that the underlying collection will not be mutated while the iterator traversal is in progress. To ensure that other threads will not mutate the collection during traversal, the iterating thread should be sure that it has exclusive access to the collection for the entirety of the traversal. Typically, exclusive access is ensured by synchronizing on a lock -- and the class's documentation should specify which lock that is (typically the object's intrinsic monitor)

Thread-compatible: Instances of the class provide no synchronization. However, instances of the class can be safely used in a concurrent environment, if the caller provides the synchronization by surrounding each method (or sequence of method calls) with the appropriate lock.
eg:ArrayList,HashMap,java.text.SimpleDateFormat

Thread-hostile: Instances of the class should not be used in a concurrent environment even if the caller provides external synchronization. Ideally, classes should not be written that are thread-hostile. Typically a thread-hostile class is accessing static data or the external environment.
eg:Class which calls System.setOut().

The developer for a java class should mention the thread safety in terms of one of the levels mentioned above in the Javadoc. This would help the people who would be maintaining the class in future and also the consumers of these classes.

References : Bloch, J. (2001), Effective Java: Programing Language Guide, Addison-Wesley.

Thread-related Exceptions in Java

This post summarizes in one place the thread-related exceptions in Java.



1) IllegalMonitorStateException is thrown when
  • the wait, notify, or notifyAll methods are called by a thread that has not locked the associated object.
2) IllegalThreadStateException is thrown when
  • the start method is called and the thread has already been started.
  • the setDaemon method has been called and the thread has already been started.
  • an attempt is made to destroy a nonempty thread group.
  • an attempt is made to place a thread into a thread group (via the Thread constructor) and the thread group has already been destroyed.
  • an attempt is made to get the exit value of a nonterminated Process.
3) SecurityException is thrown by the security manager when
  • the Thread constructor has been called and it is requested that the thread to be created be placed into a thread group for which it has no security permission.
  • a stop or destroy method has been called on a thread for which the caller does not have the correct permission for the operation requested.
  • the ThreadGroup constructor has been called with a parent group parameter for which the calling group has no permission.
  • a stop or destroy method has been called on a thread group for which the caller does not have the correct permission for the operation requested.
4) NullPointerException is thrown when
  • a null pointer is passed to the stop method.
  • a null pointer is passed to the ThreadGroup constructor for the parent group.
5) InterruptedException is thrown when
  • a thread that has made a join method call is woken up by the thread being interrupted rather than by the target thread terminating.
  • a thread that has made a wait method call is woken up by the thread being interrupted rather than by a notify or notifyAll.
  • a thread that has made a waitFor method call is woken up by the thread being interrupted rather than by the target process terminating.

Friday, January 22, 2010

How to deal with deadlock ?

There are three possible approaches to dealing with deadlock:

  • Deadlock prevention — Deadlock can be prevented by ensuring that at least one of the four conditions required for deadlock never occurs. For example, by using sharable resources, never holding one resource while waiting for another, making resources preemptible, or by imposing a strict logical ordering on resource allocation requests so that all threads request resources in the same order.

  • Deadlock avoidance — If more information on the pattern of resource usage is known, then it is possible to construct an algorithm that will allow all the four conditions necessary for deadlock to occur, but which will also ensure that the system never enters a deadlock state. A deadlock avoidance algorithm will examine dynamically the resource allocation state and take action to ensure that the system can never enter into deadlock. Any resource allocation request that is potentially unsafe is denied.

  • Deadlock detection and recovery — In many general-purpose concurrent systems, the resource allocation usage is a priori unknown. Even if it is known, the cost of deadlock avoidance is often prohibitive. Consequently, many of these systems will ignore the problems of deadlock until they enter a deadlock state. They then take some corrective action (for example, by aborting a concurrent activity and preempting its resources.)

What are the conditions which can cause deadlock

There are four necessary conditions that must exist if deadlock is to occur.The conditions are as follows:

  • Mutual exclusion — only one concurrent activity can use a resource at once (that is, the resource is nonsharable or at least limited in its concurrent access); using locks as an example, mutual exclusion locks as opposed to read/write locks are more likely to cause deadlock.

  • Hold and wait — there must exist concurrent activities that are holding resources while waiting for others resources to be acquired; with locks, concurrent activities must be holding locks while waiting to acquire new locks.

  • No preemption — a resource can only be released voluntarily by a concurrent activity; the locks acquired by a concurrent activity cannot be forcibly taken away from it by another activity.

  • Circular wait — a circular chain of concurrent activities must exist such that each activity holds resources (has locked the objects) that are being requested by the next activity in the chain

Wednesday, January 20, 2010

Common Concurrency Challenges

1) Race Condition
Multiple threads access the same shared data without the appropriate locks on shared data.When this condition occurs, one thread may inadvertently overwrite data used by another thread, leading to both loss of information and data corruption.Minimun requirement for Race Condition to occur is 2 threads and 1 shared data object amongst them.

2) Thread Block
A thread calls a long-running operation while holding a lock thereby preventing the progress of other threads.When this condition occurs, application performance can drop dramatically due to a single bottleneck for all threads.

3) Deadlock
Two or more threads wait for locks in a circular chain such that the locks can never be acquired. When this condition occurs, the entire software system may halt, as none of the threads can either proceed along their current execution paths or exit. Minimum requirement for deadlock to occur is 2 threads and 2 shared data objects amongst these 2 threads.