Before we go ahead and explore Apache Commons EqualsBuilder and HashCodeBuilder we must know the relationship between equals and hashCode.
java.lang.Object which is the super class of all Java classes has two very important methods defined in it. They are –
- public boolean equals(Object obj)
- public int hashCode()
These two methods are very important when our classes deal with other Java classes such as Collection API used for searching, sorting, comparison and eliminate duplicate objects from a set.
Utilization of memory efficiently is vital for any application’s performance. Even all modern development environments provide automatic memory management capability though Garbage Collection (GC) mechanism, we cannot completely rely on GC.
In several situations we create multiple objects and most the objects within the scope of the application, but we seldom use those objects. Even worse sometime we open database, network connections or file stream but do not close them. These mistakes eat up valuable system memory resulting degradation of application performance.
Even those there are multiple memory profiling tools available in the market we can still check how our application is performing in context of memory utilization by creating our own small utility class.
Let’s try to develop a class that can be hooked to any of the application which keeps on checking our applications heap memory utilization in every 30 second interval.
To develop this utility we need to take care three important Java classes:
java.lang.Runtime– Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
java.util.TimerTask– A task that can be scheduled for one-time or repeated execution by a Timer.
java.util.Timer– A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.