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

    How to add custom user attributes in keycloak and access them in spring boot application

    Sometime it may be possible you want to add more parameters to standard registration page of keyloak for your users and aaccess that data in your spring boot application. This artical will show step by steps on how to add such extra attributes. What is Keycloak Keycloak is an open source software product to allow single sign-on with Identity Management and Access Management aimed at modern applications and services, to learn more visit  h ttps://www.keycloak.org/ What is Spring boot Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". To learn more visit  https://spring.io/projects/spring-boot To add an extra attribute in keyclkoak server you will need to edit actuall html template and then registaer new attribute in json response so that it will be available on client. Edit HTML template Lets assume we want to add mobile number on default registration page. Go to Keycloak home installation directory edit fil

    How to create java maven project in intelij

    Open intellij Create a new java maven project in intellij . Select Maven type, Select JDK you want to use and click Next. Enter GroupId and ArtifactId, click Next Select project Location and click finish Intelij will display a warning, just press Ok. Once project is created , a popup may appear asking for auto import, select "Enable Auto Import" You will have a project created, looking something like this Note: I have created these instructions using, Instruction for other OS or intellij Version shouldn't be much different, if you need instruction for other version leave a comment and i will try to come up with another set of instructions. Intellij 2017.2.1 Java 1.8 Mac OS

    How to git without finger print confirmation

    In this article i will explain how you can run git clone command on a machine without having to accept sh key finger prints manually. Lets you want to write a script to initialise a developer machine, which require you to git clone various projects from gitlab or github or any other git repo. First time you run a git clone command, it will ask you if you accept the signature and it need to be part of automated script, its not nice thing to have. Here is the solution. First you download the git host's key then create its finger print then check this finger print against known valid finge rprint If its good then move downloaded ssh key to ~/.ssh/known_hosts file else throw error ssh-keyscan github.com >> githubKey ssh-keyscan gitlab.com >> gitlabKey export githubfinger=$(ssh-keygen -lf githubKey) export gitlabfinger=$(ssh-keygen -lf gitlabKey) echo $githubfinger echo $gitlabfinger if [[ $githubfinger == *"nThbg6kXUpJWGl7E1IGOCspRomTxdCARL