Posts

Showing posts from August, 2010

Two uses for enum most people forget.

The enum can be used to create enumerated values and even many instances with different implementations but there are two simple uses which are appear to be under utilised. The utility class A utility class is a class with no instances. To declare this as an enum is trivial. You just give it no instances. public enum MyUtils {; public static String process(String text) { /* ... */ } } The singleton A singleton which is a class which has one and only one instances, ideally loaded lazily. Using the lazy loading of classes can give the same benifit in a simpler/thread safe way. To declare a singleton as an enum, you give it one instance and only reference the instance rather than the class. public enum Singleton implements SingletonService { INSTANCE; public String instanceMethod(String text) { /* ... */ } } An enum can implement an interface, allowing it to be mocked out where ever the interface has been used.

Gotcha: Save on objects and get more Full GCs.

We have system which is latency sensive and object light. However as we tuned the system to run less minor collections we saw the number of Full GCs increase. Some investigation found that there is a timer for RMI which checks if a GC has occurred in the last hour and if it hasn't performs a Full GC. :P If you are not really using RMI, this is undesireable. There are two command line options which can be increased to reduce the number of these only-for-RMI Full GCs. Increasing just one has no effect. The defaults are: -Dsun.rmi.dgc.server.gcInterval=3600000 -Dsun.rmi.dgc.client.gcInterval=3600000 For more details bug id 6200091 See line 109 of The sun.misc.GC.Daemon class