Sunday, February 12, 2012

Algorithem Matters to Performance

If we want to find all Prime number within a positive range, how would you figure it out? I want to share with you with a simple code on how algorithm matters to performance: I am using Ruby to demo the difference between 2 ways to organize your code and how they impact the performance: Without any optimization:
 require 'ruby-prof'  
 def findprimenumber(uppernumber)   
  count =0;   
  for i in 3..uppernumber   
   primebool = true;   
   for j in 2...i   
    if (i%j!=0)   
    next;   
    else   
    primebool = false;   
    break;   
    end   
   end   
   if(primebool)   
   count= count+1   
   end   
  end   
  puts("total prime count:#{count}");   
  puts RubyProf.measure_process_time   
  end   
With optimization:
 require 'ruby-prof'   
 def findprimenumberopt(uppernumber)   
  count =0;   
  for i in 3..uppernumber   
   primebool = true;   
   if i%2==0   
   primebool = false;   
   next;   
   else   
   *for j in 3..Math.sqrt(i).floor+1 *  
    if (i%j!=0)   
    next;   
    else   
    primebool = false;   
    break;   
    end   
    *j=j+2;*  
   end   
   end   
   if(primebool)   
   count= count+1   
   end   
  end   
  puts("total prime count:#{count}");   
  puts RubyProf.measure_process_time   
  end   
Given the uppernumber to 100000 The performance result without optimization code :47s The performance result with optimization code :0.9s Also you may try more optimization way such as divide and conquer by parallel computing...

Sunday, February 05, 2012

The Parenthesis Matched?

 def parentmatched(inputstring)  
  while inputstring != nil  
   p inputstring  
   break if (inputstring.gsub!(/\(\)/, '')==nil)  
  end  
  if inputstring.eql?('')  
   puts "matched"  
  else  
   puts "non-matched"  
  end  
 end  

 parentmatched("((())(())()(()))()");  

MAT(Eclipse Memory Analyzer Tool) Usage Simple Check list

Those are some basic steps to do heap dump analysis using MAT when there comes an OOM or memory leak issue:

Pre-condition:Config MAT correctly(you may edit JVM Heap size if it is not sufficient to open a .hprof file, for example, modify "-vmargs-Xmx1280m" to MemoryAnalyzer.ini)
1. Quick Overview on Leak Suspects Report: it will give you some indicator to the leak suspects or the memory issue, it shows the biggest retained heap size after grouping by the same instance name
2. Quick Overview on Overview Pane: it lists the each biggest individual object by retained heap size
3. Open histogram to list number of instances per class, within the histogram, you can check:

  • Order by Shallow heap size to see if there is a particular object taking too much memory size, then drill it down by showing its dominator tree view for further analysis
  • Order by Retained heap size which represents the amount of memory that will be freed by the garbage collector when this object is collected, then drill it down by showing its dominator tree view for further analysis
  • Order by Objects Count to see if a lot of objects are accumulated, and take the further analysis by viewing the "merge shortest paths to GC roots" (Who is referencing them)
4.Some specific class needs to be paid attention to, for example "org.apache.catalina.core.ApplicationHttpRequest", it includes the very useful attributes for trouble shooting and reproducing the problems which are RequestURI and queryString

--Cheng Chi