java - Is there any use in caching very small objects? -


taggedlogger has string field - tag.

public class taggedlogger {      private final string tag;      public static taggedlogger forinstance(object instance) {         return new taggedlogger(gettagofinstance(instance));     }      public static string gettagofinstance(object instance) {         return gettagofclass(instance.getclass());     }      public static taggedlogger forclass(class<?> someclass) {         return new taggedlogger(gettagofclass(someclass));     }      public static string gettagofclass(class<?> someclass) {         return someclass.getname();     }      public static taggedlogger withtag(string tag) {         return new taggedlogger(tag);     }      private taggedlogger(string tag) {         this.tag = tag;     }      public void debug(object obj) {         log.d(gettag(), string.valueof(obj));     }      public string gettag() {         return tag;     }      public void exception(string message) {         log.e(gettag(), string.valueof(message));     }      public void exception(throwable exception) {         log.e(gettag(), string.valueof(exception.getmessage()), exception);     }      public void exception(throwable exception, string additionalmessage) {         log.e(gettag(), string.valueof(exception.getmessage()), exception);         log.e(gettag(), string.valueof(additionalmessage));     }      public void info(object obj) {         log.i(gettag(), string.valueof(obj.tostring()));     }  } 

and taggedloggers using cached (or create new , put in cache) taggedlogger instances:

public class taggedloggers {      public static final taggedlogger global = getcachedwithtag("global");      private static final map<string,taggedlogger> cache = new hashmap<string, taggedlogger>();      public static taggedlogger getcachedforinstance(object obj) {         return getcachedwithtag(taggedlogger.gettagofinstance(obj));     }      public static taggedlogger getcachedforclass(class<?> someclass) {         return getcachedwithtag(taggedlogger.gettagofclass(someclass));     }      public static taggedlogger getcachedwithtag(string tag) {         taggedlogger logger = cache.get(tag);         if (logger == null) {             logger = taggedlogger.withtag(tag);             cache.put(tag, logger);         }         return logger;     }  } 

is there use in taggedloggers class?

actually use taggedlogger logging using arguments tags. i.e.:

public class fragmentutils {     public static void showmessage(fragment fragment, string message, int toastduration) {         taggedloggers.getcachedforinstance(fragment).debug(message);         context context = fragment.getactivity();         if (context == null) {             return;         }         toast toast = toast.maketext(context, message, toastduration);         toast.show();     } } 

so, caching taggedlogger instances helps me avoid lot of unnecessary instances.

but, should so?

caching of existing instances can lot or can kill performances.

when creating new instance have consider 2 factors :

  1. the time setup instance itself, allocate ram, initialize fields , execute constructor
  2. the time taken garbage collector cleanup after instance not reachable anymore

this used lot of time on older jvms, not garbage collector has been improved , creating , throwing away small instances not big problem before, still has it's cost. don't know how android jvms have been optimized.

in case depends on how create , throw away these instances, said often.

when instead reusing them, have consider 2 factors :

  1. the time lookup existing instance, map lookup
  2. the ram kept full of unused instances

so, in case, depends on how many different instances have. if have thousands of taggedlogger's looking map , keeping stuff in ram hurt performances more creating , throwing away.

if taggedloggers around hundred(s), better caching, if go thousands, better instantiate , throw away.

however, question wether need taggerlogger. if have tag string, can't call logger method or have (possibly static) façade in front of it, instead of instances contains information (the tag string) have?


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -