Monday, September 22, 2014

10 reasons why windows 8 sucks

Microsoft windows operating system is very popular and majority of people are using this. Initially, this operating system looks good, and pretends to be user friendly. I also started learning computer using windows and I am pretty sure the previous windows OS's were quite nicer. 

When I started using computer, Windows 98 was very popular at that time. Later, we got Windows 2000 which was little bit improved version from windows 98. Then a drastically improved OS Windows XP appeared in the market. That got popular also and I used windows XP until Window 7 released. Yes, I preferred windows 7 very much and started using it from RC version. Till Windows 7, I did not have any complain with Microsoft operating system.

Now, lets go to the actual topic: Windows 8. Why the fuck Microsoft developed this opearting system. I have no idea, why they want something new which sucks. I feel very uncomfortable using this OS. I think you guy also have the same situation; I have not found any guys who is happy with this OS.

Now, I will list out some points which are worth mentioning here:

1) Cost: 
Everybody pays money to use windows OS because it is not free. So, even if you buy a laptop, you have to pay for the OS, even if you don't want to use it. I rarely used windows 7 and windows XP that came with my laptops. Its rare that we can buy a laptop without windows system. So, windows is making a sort of compulsion to pay for its operating system even if you are not interested.  It sounds like b**sh*t.

2) Booting:

Windows always try to be on the top of everybody, and not friendly with other operating systems. You know what I mean. Its the real problem when you install windows system, you have to compromise by erasing the previously installed operating systems. I have this problems several times, I install windows, my previously installed ubuntu and mint system gone! Then again I have to install those. And the latest operating system i.e. Window 8 even doesn't have that facility. We can not even install other operating system so easily. I believe this means microsoft is forcing users to use windows operating system. I got a new notebook with windows 8 pre-installed, and I did hard disk partition and installed linux mint. Then the problem was, I have to go to book and change settings there to select linux mint. WTF! I tried to display list of OSs to display while booting, but I was unsuccess. So, I just go on using window 8 which is not my favourite OS. 


3) Upgrade:

The upgrade system of windows also sucks. You are hurry to go somewhere, and want to turn off your computer, then computer says "don't turn off you machine, updates are going on!" Why they don't want to let you go after turning off??
I have waited many times till updates finishes, that was very bad experience. And updates are too slow, I have very fast (50 mbps) internet speed, still it takes several hours for updates.  


4) Bulky
Windows system are bulky, they use hard disk space of around 20 GB after you completely install it. So, to have a windows system installed you have to have at least 25 GB of hard disk space. If you wanna install more software, you need more. And it is true, that minimum space required for windows system is more than enough for linux based system. I have separated 30 GB space for linux mint and using for more than one year, I am not needing any extra memory. 

5) Upgrade to Windows 8.1 
I don't like so call Metro system of windows 8. So, I wanted to upgrade my system to get window 8.1 which I heard is improved and windows 7 like desktop is shown in the first place. Unfortunately, I got a lot of problems, till now I could not do it. Its seems like driver related problem but the acetic question is why they do not these things before they deploy it, they are money to the user and again there are so many problems. I have spend almost 20 hours upgrading to windows 8.1, still no luck. I did not face any problems with ubuntu and linux mint which are open source free operating system. 


6) High System Requirements
The system requirements are exponentially increasing from windows 98 to windows 8. Really, I dont like it, windows system sucks a lot of system resources such as memory, CPU. There is clear different when compared with linux mint which looks like windows, it consumes far less memory and CPU doing the same task. 

7) Internet Explorer:
One of the unanswered question from microsoft is why they can not improve internet explorer. It looks like 98's design, and seems like there is no improvement. Same bulky, not looking so user friendly like modern browsers- mozilla or chrome. After I install windows system, my first task is download chrome from internet explorer and disable internet explorer. I feel terrible even if I accidently click on internet explorer, it annoys me to properly close it.

8) Unwanted Services 
Everybody knows windows system run a lot of unwanted programs and services in the background. One of the reason why windows is slower is because of this. Although user can turn off unwanted service, but it is very hard to determine which service they want and which they do not want. 

9) Antivirus
Windows OS is prone to virus attack. So, you have to buy antivirus software along with windows. I guess there is some collaboration between antivirus company and microsoft, that is why, antivirus company prepares antivirus for microsoft windows. Interesting, why can not  microsoft make their system secure enough so that there is no need of antivirus. 

10)  Slow

Windows operating systems are slow as compared to other systems, booing itself seems slower (windows 8 did some trick to make it faster booting, that is just trick, not reality) and installation, updation process are so lengthy, I hate it. 

Tuesday, August 12, 2014

Java Performance Monitoring and Testing

After a couple of months gap, I am continuing this blog. This time, the topic is not about development, this is a bit complicated but interesting topic, and I have spent quite a lot of time for performance monitoring in java.

Java is very popular programming language and we can develop any type of application from desktop, web, mobile, embedded and many more. The use of advanced IDE's (Eclipse for example) make the development process very fast, easy and with a lot of customization preferred by developer. The software development, nowadays, is so fast that most of the time, we forget about performance that is how well and how consistently the application works for the long run after deploying. 

We have to deploy applications which has to run for several months or even several years! To make the development process faster, we use a lot of third party libraries which might not have been fully tested to work for long run. Sometimes, we program incorrectly, or we forget to properly manage the created objects, which actually affects the application for long run, for example, memory leak. It is the most prevalent issue found in applications which run for  a long time. This article does not go deep into performance analysis theory, but gives quick idea on how performance analysis in java is carried out using available tools.

1) JConsole

JConsole is a simple performance monitoring tool which displays alot of useful information of a running java application. I am using Java 1.7 and JConsole is included in this version. You have to be sure, I guess old java version do not support JConsole, if that happens, you have to upgrade you java.

For local testing, its really straight forward, JConsole provides list of java applications and we have to select one. Then JConsole displays memory, threads, classes related to that application. A nice presentation of these information in graph or table, makes it easy to see if there is any subtle performance problem. If we let it running for long time, we can easily notice if there is any memory leak or system problem with heavy consumption of processor by the application, maybe my huge number of threads creation.

   Fig:   JConsole

We can remotely connect to the application running in different system through network. For that we have to enable Remove Management and Monitoring Service (JMX) in JVM. If you are running tomcat, its rather straight forward. We just append the following text in the setenv.sh file in $CATALINA_BASE directory.

export CATALINA_OPTS="-Dcom.sun.management.jmxremote
                                               -Dcom.sun.management.jmxremote.port=
                                               -Dcom.sun.management.jmxremote.authenticate=false
                                               -Dcom.sun.management.jmxremote.ssl=false
                                               -Djava.rmi.server.hostname="


If you want to increase heap memory size, you can append the following line.(this is optional)

export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"

export CATALINA_OPTS="$CATALINA_OPTS -Xmx1024m"

Then you have to restart tomcat server. Then we can remotely connect the application running in remote system by using the IP and PORT specified above. 

Since JConsole is quite simple, it is used to find out if there are any performance issues in the application. It is the first step for performance monitoring. Normally we run JConsole for a couple of days, and study the performance graph. From the graph, we can speculate whether there are any performance issues or not. If we know that there are performance issues, then the next step is to go for more advanced monitoring tool which is capable of finding specific object or specific thread causing the performance issues. For example, if the application is spawning a lot of threads and creating CPU overhead, then we have to find out which specific threads are creating such problem, or  a lot memory is consume by objects and Garbage collector is not successful to release the memory, then we have to find out which specific object is causing such a memory leak.

2)  JVisualVM

This is more advanced tool and this is built-in with latest Java. It includes all functionality provided by JConsole, apart from that we can create thread dump, heap dump and application snapshot. The dump files can be analysed later using some other dump analyzer tools to get more performance monitoring. The dump analyzer tools such as JProfiler, YourKit are very nice commercial tools(you can have around 10 days of trial use). I feel really excited if we can find some free analyzer, then we should not bother about paying and licencing.


For remote connection, as mentioned in JConsole, we have to enable JMX in JVM.

3) Automation Approach

Yes, we can dump heap memory as a file, if we want to test it for whole night, but nobody stays in front of computer dumping memory.. lol. So, we should have mechanism that we can dump heap memory in regular interval, so that we can figure out which object is getting bloated with time; also can track the number of threads, and their characteristics.

There is a command line tool jmap which dumps the heap memory. Jmap needs process id to work, so we find out pid of the application from the list of java application using "jps" command from terminal.

#Find id of java applicaiton
jps 
#dump
jmap -dump:format=b,file=FILE_NAME PROCESS_ID


    This is complete script that can dump heap memory in regular intervals until specified dump files are     obtained.
 #!/bin/bash
#file:heapdump.sh
if [ $# -eq 0 ]; then
    echo >&2 "Usage: heapsump.sh  [ [  [  ] ] ]" 
    echo >&2 "    Defaults: run_user=root, count = 10, delay = 0.5 (seconds)" 
    exit 1
fi
pid=$1                  # required
user=${2:-root}         # defaults to root
count=${3:-10}          # defaults to 10 times
delay=${4:-0.5}         # defaults to 0.5 seconds
while [ $count -gt 0 ]
do
    sudo -u $user jmap -dump:format=b,file=dump.$pid.$(date +%Y%m%d%H%M%S.%N) $pid
    sleep $delay
    count=$(( $count-1 ))    
done
     Call this script this way: 
sh heapdump.sh PID [root [15 [10]]]
     All arguments are optional except PID.   Other parameters are user, count and interval(delay for next       dump).

4) Eclipse Memory Analyzer

  Till writing this text, I found Eclipse Memory Analyzer plugin which can be installed using
  http://download.eclipse.org/mat/1.4/update-site/

After installing this, we open memory analyzer perspective where we can dump heap memory from locally running VM. Not only this, you can open previously saved dump files and analyze. So, this is a great plugin, really great because it is free! I have to explore more to write more. So, later I will try more interesting features provided by this plugin. 

I could not perform online analyzing using Eclipse Memory Analyzer. However, this is possible using JProfiler. I guess Eclipse Memory Analyzer is sufficient for fully analysis of heap dumps. Again, JProfiler sucks when the heap size is huge(around a couple of gigs).

Friday, February 14, 2014

Defining Persistent Objects

The persistent objects can be defined either in xml file, or using annotation. The annotation method is easy to define simple relationship while defining in xml file provides more flexibility.

This article demonstrates, rather shortcut, how we can create persistence objects:

//Base Class
@MappedSuperclass
public abstract class BaseClass implements Interface{
@Id
@Column(name = "id")
@GeneratedValue
@Override
private int id

@Transient
private String unwantedField;

public int getId(){
    return id;
}

public void setId(int id){
    this.id=id;
}
}

//Child Class
@Entity
@Table(name = "table_name")
public class ChildClass extends BaseClass{

@Column(name = "user_name")
private String useName;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "id")
@Override
private List comments;
}


//Associated class
@Entity
@Table(name = "another_table")
public class AnotherClass {
@Id
@Column(name = "comment_id") //this can not be "id", because "id" is referenced from ChildClass
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = "id") //this id is child class identifier column
private ChildClass child;

}

In above example, we implemented one to many relationship from ChildClass to AnotherClass instances.(Getters and setters not shown above). So, the "id" in "another_table" is foreign key from the table "table_name".

[Note: Annotations should be either at properties definition or at getters(CAN'T  be mixed).]





Accessing Persistent Objects

In the previous article, I have discussed how to create session instance. This article demonstrates how session object can be used to access persistence objects, insert, update or remove objects.
[Assumption: we have session object, and we have to close transaction and session after we complete operation]

1) Insert, Update or Delete persistence object

  session.saveorUpdate(object);
  session.delete(object);

2) Retrieve a collection of data
   i) If you want all persistent objects
       //using hibernate query
       List list=session.createQuery(HQL).list();
       //using sql 
       List list=session.createSQLquery(SQL).list();//gets raw
       List list=session.createSQLquery(SQL).addEntity(className).list();//entity objects

   ii) If you want selected object only (similar to WHERE clause in SQL)
 
     String HQL="SELECT * FROM ENTITY entity WHERE entity.fieldName=:fieldname";
     session.createQuery(HQL).setString(fieldname, value).list();
 
     For SQL, we create the sql query to get the list.      

    Criteria is more elegant for selection.
   
      Criteria criteria = session.createCriteria(className);
       criteria.addOrder(Order.desc(fieldName));
       criteria.add(Restrictions.ge(fieldName, value));
       criteria.add(Restrictions.le(fieldName, value));
      criteria.setMaxResults(limit);
      
      // Also define union and intersection
       Disjunction disjunction = Restrictions.disjunction();
       disjunction.add(Restrictions.eq(fieldName, "#BLABLA#"));
       criteria.add(disjunction);      
       Conjunction conjunction=Restrictions.conjunciton();
       conjunction.add(Restrictions.eq(fieldName, "#BLABLA#"));
       criteria.add(conjunction);    

        criteria.setFetchMode("whatever", FetchMode.SELECT);

     List objects= criteria.list(); 

3) Retrieve specific object

We have to note here that all persistence objects have their own id. So,  with the id, we can get object

 Object object=session.get(className, id);

Hibernate Basics

Recently, I faced a lot of problem implementing persistence of objects with the database using Hibenate persistence API. If we know exactly, its very easy and straight task implement persistence on the objects with database, otherwise, we might get very peculiar problems where it takes a lot of time to get out of it.

In this article, I will present my experience creating persistence objects using PostgreSql database. I will start from beginning so that you can directly use the source code in your implementation.

1) Get Data Source
Use the following method to get datasource. We use this later when creating properites.
private DataSource getDataSource(String host, String database, String userName, String password) {
PoolProperties poolProps = new PoolProperties();
poolProps.setUrl("jdbc:postgresql://" + host + "/" + database);
poolProps.setDriverClassName("org.postgresql.Driver");
poolProps.setUsername(userName);
poolProps.setPassword(password);

poolProps.setJmxEnabled(true);
poolProps.setTestWhileIdle(false);
poolProps.setTestOnBorrow(true);
poolProps.setValidationQuery("SELECT 1");
poolProps.setTestOnReturn(false);
poolProps.setValidationInterval(30000);
poolProps.setTimeBetweenEvictionRunsMillis(30000);

poolProps.setMaxActive(75);
poolProps.setMaxIdle(25);
poolProps.setInitialSize(3);
poolProps.setMaxWait(10000);
poolProps.setRemoveAbandonedTimeout(60);
poolProps.setMinEvictableIdleTimeMillis(30000);
poolProps.setMinIdle(10);

poolProps.setLogAbandoned(true);
poolProps.setRemoveAbandoned(true);

poolProps.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

DataSource dataSource = new DataSource();
dataSource.setPoolProperties(poolProps);

return dataSource;
}


2) Create properties object

Properties props = new Properties();
props.put("hibernate.connection.datasource", getDataSource(hostname, database, username, password));
props.put("hibernate.cglib.use_reflection_optimizer", true);
props.put("hibernate.show_sql", false);
props.put("hibernate.hbm2ddl.auto", "update");
props.put("transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
props.put("hibernate.jdbc.batch_size", batchSize);
props.put("hibernate.default_batch_fetch_size", fetchSize);
props.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");


3) Create Configuration

Configuration config = new Configuration();
config.setProperties(props);
//class registration
config.addAnnotatedClass(className);

If we have to register all classes in the specified package, then we have to get all classes inside the package.

4) Create session factory

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(props).buildServiceRegistry();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);

5) We have to get session for persistent objects, so we get this by:

Session session = sessionFactory.openSession();
session.setFlushMode(FlushMode.COMMIT);

6) While executing query

Session session0sessionFactory.openSession();
Transaction trans=session.beginTransaction();
session.delete(object);
trans.commit();
session.close();


Creating persistence objects will remove the bother of thinking the database structure and tables. We just care about objects, and the back end database operations are carried out by hibernate API itself.


Automating Jobs in Linux System (Crontab)

We install applications and services in the Ubuntu system and they go on running all the time. So, it is not guaranteed that they run perfectly. Even, some services generate huge log files, or some services go on consuming more and more memory, finally, making the system crash with memory leak. So, to minimize those problems, we implement the auto-restart in the system.

Since, we have different system configuration (such as services, hardware and memory), the way we define auto-restart is different for different system, so we don't make it built-in image characteristics.

The settings of auto-restarting of server is quite easy task if we are familiar with Linux's crontab application.

Edit

crontab

We can define the automated tasks using crontab command from the terminal as below:
crontab -e
Then we have to select the preferred editor, after that we go into the editor where we define the tasks to run at specified time. We have to add a line for each task in the following format:
minute hour day_of_month month day_of_week command
Note: every users have different crontab file generated in /var/spool/cron/crontabs with the name of the user.
So, that if enough for theory. Now, if we add the following line
20 01 * * 1 sudo reboot
Then it restarts the system every Sunday at 01:20 am.(The user should have super user privilage to restart the system.)
We can change the values according to our requirements.

Edit

/etc/crontab

We can define user wise cron tasks by editing /etc/crontab file. The format is same as above, only we have to provide the user name can execute the defined task, for example:
20 01 * * 1 frietec sudo reboot
So, the user frietec reboots the system every Sunday at 01:20 am.

[Note: the command provided should be executable by the provided user, otherwise, nothing happens.]

If you look into the file /etc/crontab file, you will notice there is already some automated tasks in the folders /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly defined in the file.  So, an alternative will be keep the task(script) in the specified folder to execute at the define time in /etc/crontab file.

clear log files

Log files in linux system are located under /var/log directory. And the long time running system, in case there is some problem, will grow log files size. So, to prevent the system from crashing, we have to implement the mechanism to clear log files.
A simple method to clear log files is to place following line in the crontab job:
0 11 * * 1 find /var/log -type f -delete
This line will find all log files inside /var/log directory and deletes.
The above command can be executed by root user only, so we keep this in root crontab.