Skip to main content

Few tips to write low latency programs

People ask me can you write Low latency Java programs, and i always feel low latency is a relative terms and based on lots of things, not just my java code. But then i thought as a programmer i should think how my code could use my underlying resources in better way and make JVM performance better.
(My JVM or java code cant make harddisk run faster but i can write programs so that i can access hardisk less :) )


Few tips which i think we should keep in mind while writing code which should have low latency
  • Initialise/create Collection objects as actual size as near as possible.
If you can guess actual size of collection at run time, then its always better to create collections of that particular size.
e.g. if i know my list will have 100 items(approx) and those will be added at once, then its always good to create list like this
List<Object> list = new ArrayList<Object>(100);
instead of
List<Object> list = new ArrayList<Object>();

and for Hash based collection this is very important, as after resizing, rehashing is done for all elements which could affect your performance

  • make all private functions as static, though i expected that run time linking of private methods should be same as static methods as there must not be any lookup for private method actual implementation. No one can override private method
  • Maps usage , if you are using HashMap which will be read/write by many threads and you are planning to use synchronise to that, then better look into ConcurrentHashMap and initialise it properly with size and partition(default 16).
  • Which implementation of List to be used, Array List or Linked List, think hard before using one of them.
  • Which implementation of Set to be used.
  • Which Implementation of Map to be used.
  • Object reuse, more the object reuse less the task for Garbage Collector.
  • Make sure you give enough memory to JVM < the memory available from machine underlying, else you will have too much latency in memory swaps from disk.
  • Look into Garbage collector settings and see which one you should be using. Ofcourse concurrent GC would be better if you are running multiple processor :).
  • You can add Strings like this String a = b + c +d +e +f, earlier java version used to create lots of intermediate Strings like b+c, then b+c+d etc etc, but i saw in latest java compiled file that generated byte code, generate this code as if you have used StringBuilder, so don't worry about using StringBuilder or StringBuffer :). But should be used if you have dynamic Strings to be added.
  • If traversing a ArrayList then try to avoid new style looping for(Object oneObject:list), instead loop through using list.get(i) and for loop as
for(int i=0;i<list.size();i++){
list.get(i);//Do something with element
}

Comments

Popular posts from this blog

Validate HTML as XHTML in Intelij

When working in intelij with HTML, sometimes we need to make sure our HTML is XHTML compliance, no un ened tags etc.
i.e. we should not have

<img src="myimage.jpg> If You put above code in an HTML and run it in browser it will work fine, but if you are using some server side framework like Thymeleaf with Spring, it will generate error saying <img> tag must be terminated.

By Default intelij will not complaint about this and when you are writing HTML you may ignore the error and will get the error when you actually run it with in server. Or worst some HTML developer is writing the code HTML code for you and then you will have to fix all these errors.

Intelij can help you to make sure you write a valid HTML(XHTML). You can enable XHTML validation for HTML files by following below steps.

Go to Settings(Windows)/Preference(mac) in Intelij.Search for "File Types"Find HTML as shown in image below. You will see HTML has some extension associated with like *.html, *…

Understanding Equals and hashCode contract,and what can go wrong

SO what about Equals and hashcode?
Most people will say these are the two basic function available in Object class and could be called from any Class object created in Java. Well thats true but there is more then this.
First take a look at HashCode:
Every JVM provides a good hashCode implementation mostly based on the object's memory location and for two different object(by location) it produce different hashCode.
And at the same time Equal method also consider two different memory location object as Not Equal. So at core two are in a contract and works great.

But as soon as start writing code and write a class and create a state of a class(means create few data variables in a class), the core contract of hashCode and Equal doesnt work any more.
e.g.
Class MyFirstCLass{ int value; public MyFirstCLass(int value){ this.value = value; } } MyFistCLass obj1 = new MyFirstCLass(10); MyFistCLass obj2 = new MyFirstCLass(10);
Now obj1 and obj2, theoratically should be equal as both holds …

Difference between ArrayList and LinkedList

What is the difference between ArrayList and LinkedList

First of all lets see what Interfaces they implement.
ArrayList (See on Oracle/sun site)SerializableCloneableIterable<E>Collection<E>List<E>RandomAccessLinkedList(See on Oracle/Sun site)SerializableCloneableIterable<E>Collection<E>Deque<E>List<E>Queue<E>By looking at interface being implemented by these two collection, we could see RandomAccess is implemented by ArrayList and Queue<E>,Deque<E> implemented by LinkedList only. So all the differences between these two collections are because of these three Interfaces and their implementation.

RandomAccess : ArrayList can be accessed randomly, means we could access nth element of an ArrayList directly without affecting search/seek performance. e.g. list.get(n). Means get performance of nth element will never depend on how large is the ArrayList.It will remain constant.
While LinkedList cant be accessed randomly(though it does …