Java

Do you plan intelligently before you write your Application?

Plan intelligently before you write your code, as this is directly related to Garbage Collection and your Application Performance.

“If Objects are created unnecessarily and are less recyclable, it is directly proportional to more garbage created and hence garbage collection, just like in real world”.

When we are starting on a new project we mostly care about if it’s working as expected but tend to forgot about the maintenance that will be needed for that application to run smoothly.

As a result of this negligence we start seeing Memory issues on the application especially the server in which it is running, we start finding out after the application is deployed in production about Memory Leaks etc.

There seems to be couple tips and tricks based on Oracle Java Documentation for Garbage Collection Performance.I have simplified the tips below and believe that this will be beneficial for even absolute beginners.

A:

If you know what is the maximum size your collection will hold or the expected size upon construction initialization, if so set the size. This reduces the reallocation process after every Garbage Collection takes place and helps keep the re-evaluation process to minimum.  

List<Integer> employeesBadgeCount = new ArrayList<Integer>(500);

B:

A lot of enterprise level applications deal with transfer, parse, saving of large files and request and responses and run into Out of Memory related Errors and Exceptions when the JVM can’t actually distribute a buffer size of the entire file as the Garbage Collection progression is happening.

Make a habit of using native java.io package classes i.e. FileInputStream which extends abstract InputStream. Take a peek at other famous API’s and you will see this practice being implemented.

C:

Always make a habit of using Immutable Objects if it doesn’t hamper your application security.

Why Immutable: An immutable object or an unchangeable object is an object whose state cannot be modified after it is created and hence less number of object references getting created and less cleanup required.

Personal Suggestions : Make sure to use static variables and methods where possible to help with garbage collection, as static belong to that class and hence once a reference is created that reference is not created multiple times.

This is just a small portion of tips and tricks. Make a habit of visiting Oracle Java Documentation as they do have expert advice.

Leave a Reply

Your email address will not be published. Required fields are marked *