Skip to main content

Java 12 is here!

Java has switched to a 6-month release cycle, which means you don't have to wait 3 years for a major Java upgrade. It also means less and manageable changes in each major release. Java 12 has been released recently. Here is a brief overview of new features.

Switch Expressions

We all use switch statements, and we know how much it simplifies coding. But still, it required having a break statement after every case expression. As a guy who makes a lot of careless mitsakes, missing break statement after a case is one of the mistakes I make a lot.
switch(month) {
   case 1 : 
      monthStr = "January";
   case 2 : 
      monthStr = "February";
   case 3 : 
      monthStr = "March";
   case 4 : 
      monthStr = "April";

Now with switch expressions, the switch block is going to look much cleaner and robust.

String monthStr = switch(month) {
   case 1 -> "January";
   case 2 -> "February";
   case 3 -> "March";
   case 4 -> "April";

Shenandoah Garbage Collector

Java's GC (Garbage Collector) is responsible for removing unused objects from memory. Java does this by pausing the application for a while, which is not desirable by real-time applications. With the Shenandoah algorithm, pause times are significantly reduced. This is still an experimental feature which means you have to enable it manually for it to work.

GC is one of the most debated features in Java since it is known to affect the performance of the application unpredictably. It is good to know that Java is putting in more effort to solve these issues. Java 11 which was released in last September had introduced ZGC, a low-latency GC.

Improvements to G1 GC

G1 GC was introduced in Java 7. In Java 12, G1 GC comes with minor improvements. 

Abortable Mixed Collections aims to prevent long pause time during garbage collection. It does this by carrying out GC process incrementally so that it can be aborted when pause time exceeds the target time.

Promptly Return Unused Committed Memory from G1 aims to return unused heap memory to the operating system. G1 GC periodically analyzes how much heap memory is used by the application and returns the rest to the OS.


Popular posts from this blog

Programmer's Guide to Dealing With Different Time Zones

All programmers have to deal with times and time zones at some point.  When your product gets wider, it gets more and more difficult to handle and synchronize time between multiple time zones. Being well-informed beforehand might save some pain in the "head" later. I've compiled a few most important tips to take care of when dealing with time zones. The actual list is definitely much bigger. But consider this a start. Store Time in UTC Storing local time in the database is okay when your product is small and deals with just one time zone. But when you start dealing multiple time zones and DST(Daylight Saving Time) , it gets weirder. Now you have to convert your time to different time zones for different users and you have to maintain all upcoming DST changes.  Storing time in UTC can save many headaches in the future. The important reason being that the UTC timezone is never affected by DST. So always remember to store time in UTC, and apply the user's

How to deal with cache stampede in MySQL

The Cache stampede problem (also called dog-piling, cache miss storm, or cache choking) is not a big problem for small scale systems but is a common headache for large scale applications with millions of requests. It has the potential to bring down the entire system in a matter of seconds. What is Cache Stampede Consider an item that is expensive to generate, and is cached. When the cache expires, the application usually regenerates the item and writes to the cache, and continue as normal. Under very high load, when the cached item expires, multiple requests will get a cache miss and all of them will try to regenerate the cache simultaneously, which will cause a high load in the database. Cache Stampede can occur with any cache including MySQL query cache, or any external cache like Redis or Memcached. It is ok to ignore this problem when your application is small scale but very important to address the issues as you scale up.  Cache stampede can be seen as random spikes in the CPU us

Design Patterns

Do not reinvent the wheel. Just realign it. When it comes to software world, most design level problems are already solved. Design Patterns are blueprints of these solutions which you can customize and use in your application. Advantages: Tried and tested solutions be experienced developers. It helps improve developer communication. Every developer knows what a 'Singleton' is, right? Common Design Patterns Creational design patterns Abstract Factory Builder Factory Method Object Pool Prototype Singleton Structural design patterns Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioural design patterns Chain of responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy Template Method Visitor