Archive | July, 2012

Eclipse Memory Analyzer

24 Jul

Eclipse Memory Analyzer


Eclipse Memory Analyzer is used to analyze heap dumps taken from the targetted JVM. Eclipse Memory Analyzer is a useful tool to identify potential memory leaks and areas of the application which are memory intensive.

How to use

  1. Extract the folder contained in downloaded zip file
  2. Open up the file mat/MemoryAnalyzer.ini in a text editor and change ‘-vmargs-Xmx1024’ to the desired value which depends on how large the heap you intend to open is. E.g. if your max heap size is set to 2048mb you should change the value to ‘-vmargs-Xmx2048’. Although you might have to allow for some leeway (-vmargs-Xmx2500) as this dumps can usually go over the max limit when an out of memory error occurs
  3. Start the application (mat/MemoryAnalyzer.exe)
  4. Go to file > open heap dump. Eclipse memory analyzer recognizes the extension .hprof as heap dumps so if your dump does not have this extension, select All Files on the dropdown
  5. Open up the heap dump, you will see a dialog that shows the progress of the opening of the heap dump, this may take a while depending on how large your heap dump is
  6. After it has been opened you will see a dialog like this:
  7. Choosing the option Leak Suspects Report will give you an overview of what objects occupy a large portion of the heap so this is a good place to start. You will get something that looks like the following. As shown in the screenshot below we have a high level picture of potential memory leaks or inefficient memory usage due to instances of “org.apache.tomcat.util.threads.TaskThread”. It appears these instances occupy ~45% of the heap:

Another useful thing you can do to drill down further to the root cause of this problem is to click the icon ‘OQL’ as shown below.

This will open up a new tab which you can then use to make queries on the problem classes. As shown below, the query selects all objects of the specified class and displays them in a tree-like structure. Clicking on the heading ‘Retained Heap’ will allow you to sort by descending usage of memory which is probably a more useful sequence to view these objects in. You can then expand the nodes to gain further insight to where most of the memory has been allocated. The panel on the left-hand side is useful as it shows the values of each of the object fields.

Google codejam Qualification Round Africa and Arabia 2011 Problem A. Closing the Loop

23 Jul


Given a bag full of rope segments, you will build the longest loop of rope while alternating colors. The bag contains S segments and each segment will either be blue (B) or red (R). You are required to alternate between colors and because of this requirement you might not use every segment in the bag. If you only have segments of a single color, you will not be able to tie any knots and should output 0. Each segment length is provided in centimeters and each knot in the loop consumes one centimeter of length from the loop. In other words, a knot consumes one-half of a centimeter from of the two segment it connects.

Note that pieces of string that have length 1, if used in making the cycle, might get reduced to just a pair of knots of total length 0. This is allowed, and each such piece counts as having been used.


The first line of input gives the number of cases, N.
N test cases follow. For each test case there will be:

  • One line containing the value S, the number of rope segments in the bag.
  • One line containing a space separated list of S values. Each value L indicates the segment length in centimeters followed by the letter B or R to indicate the segment color.


For each test case, output one line containing “Case #x: ” followed by the maximum length of the rope loop that can be generated with the rope segments provided.


1 ≤ number of rope segments (S) ≤ 1000
1 ≤ length of a rope segment (L) ≤ 100

Small dataset

N ≤ 5

Large dataset

N ≤ 50


Input Output
6R 1B 7R 3B
5B 4R 3R 2R 5R 4R 3R
20B 20R
Case #1: 0
Case #2: 13
Case #3: 8
Case #4: 38


from sys import stdin

f = open('output.out', 'w')
for i in range(0, int(stdin.readline())):
segments = stdin.readline().split()

blue = []
red = []

for j in segments:
if j[len(j)-1] == 'B':

if len(blue) == 0 or len(red) == 0:
print 'Case #'+str(i+1)+': 0'
f.write('Case #'+str(i+1)+': 0\n')

blue = sorted(blue, reverse = True)
red = sorted(red, reverse = True)

sum = 0
segUsed = 0

length = 0
if len(blue) > len(red):
length = len(red)
else :
length = len(blue)

for k in range(0, length):
sum += blue[k]
sum += red[k]
segUsed += 2

print 'Case #'+str(i+1)+': ' + str(sum-segUsed)
f.write('Case #'+str(i+1)+': ' + str(sum-segUsed)+'\n')



change extension to .txt