Saturday, August 05, 2006

Eclipse was hanging my machine regularly -- solution -XX:MaxPermSize=128m

I have quite some plugins installed in my eclipse installation. Since a few weeks, eclipse was running mad from time to time, my XP laptop was totally starving, I was not even able to get to the task manager to kill eclipse (keyboard and mouse barley reacted). The only solution was to kill my machine :-(.

Well, but how to find out who is the bad guy? Which of the many plugins I have installed "killing me"? I was using Stack Trace, a cool application that lets you attach to eclipse and get a stack trace.

Most of the time the stack traces of the wild application ended in:
 at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.defineClass(DefaultClassLoader.java:160)
at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClass(ClasspathManager.java:498)
at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findClassImpl(ClasspathManager.java:468)
at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClassImpl

Hmm, the class loader puts the class files in a special area of the garbage collector, (perm generation). The starving (100 % CPU and being really bad) seemed to be caused by not enough memory in the perm space. To fix this, I added -vmargs -XX:MaxPermSize=128m to the shortcut that starts eclipse. That solved the problem! The sun default seems to be 64MB (I could not find information for java1.5).

But how much is the perm size really? How to find this out? Today, I found a blog entry by Igor Shabalov describing a cool tool called jconsole, that comes with jdk1.5.0, that allows you to inspect memory related information of your java applications life. All you have to do, it to add -Dcom.sun.management.jmxremote to the eclipse -vmargs. Then you can attach to eclipse and see life the different garbage collection pools and the loaded classes and threads -- pretty cool...

Friday, August 04, 2006

How to create apply a patch that contains changes in multiple projects?

Eclipse has a nice feature to apply patches (context menu of a project: Team->Apply Patch....). If you have multiple projects under CVS control, then you can create a patch that contains changes from multiple projects. The patch looks slightly odd, because id adds some additional lines that tell eclipse which projects a file belongs to. This has is new in eclipse 3.2 (bug 110481).

However if you have a patch created with diff outside eclipse that contains files from multiple projects, eclipse cannot apply the patch.

To apply my multi project patches I wrote a small Eclipse Monkey script that converts a patch on the clipboard to a Eclipse Workspace Patch 1.0, by adding some additional lines.

To install and run the script:

  • Install Eclipse Monkey (if you have not already installed it)
  • Copy the script below (including the odd lines at the beginning and the end) to the clipboard
  • In the Monkey menu do Paste New Script
  • Now you can convert patches to Eclipse Workspace Patch 1.0, by copying that patch to the clipboard and running Monkey->Tools->Convert patch in clipboard to workspace patch


--- Came wiffling through the eclipsey wood ---
/*
* Menu: Tools > Convert patch in clipboard to workspace patch
* Kudos: Michael Scharf(eclipsemonkey @ scharf . gr)
* License: EPL 1.0
*/

// pseudo imports
var ArrayList=Packages.java.util.ArrayList;
var Pattern=Packages.java.util.regex.Pattern;
var StringBuffer=Packages.java.lang.StringBuffer;
var BufferedReader=Packages.java.io.BufferedReader;
var StringReader=Packages.java.io.StringReader;
var ResourcesPlugin=Packages.org.eclipse.core.resources.ResourcesPlugin;
var MessageDialog=Packages.org.eclipse.jface.dialogs.MessageDialog;

var dnd=Packages.org.eclipse.swt.dnd;


function main() {
var patch = getFromClipboard();
if(patch == null) {
showMessage("The clipboard does not contain a string!");
} else {
// is this already a workspace patch?
// the (?m) makes sure that the ^ matches the beginning of a line
// and not just the beginning of the string!
var pattern=Pattern.compile("(?m)^### Eclipse Workspace Patch 1.0");
if(pattern.matcher(patch).find()) {
showMessage("This is already a Workspace Patch!\n" +
"It contains a line beginning with:\n" +
"\"### Eclipse Workspace Patch 1.0\"");
} else {
var messages = new StringBuffer();
patch = convertToWoskspacePatch(patch, messages);
// has it been converted?
if(patch == null) {
showMessage("The clipboard does not contain a valid patch.");
} else {
// OK let's put it on the clipboard
putOnClipboard(patch);
showMessage("Patch converted to clipboard\n\n" + messages.toString());
}
}
}
}

// show a simple message dialog
function showMessage(message) {
MessageDialog.openInformation(
window.getShell(), "Convert patch to workspace patch", message);

}
function convertToWoskspacePatch(patch, messages) {
var reader = new BufferedReader( new StringReader(patch));
var patchBeginPattern = Pattern.compile("^(---|[+][+][+]|RCS file:)\\s+([^\t\n,]+)");
var result = new StringBuffer();
result.append("### Eclipse Workspace Patch 1.0\n");
var projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
var guessedProjects = new ArrayList();
var patchFound = false;
var line;
var prevLine = null;
var hunkFound = false;
while((line = reader.readLine()) != null) {
var m = patchBeginPattern.matcher(line);
if(m.find()) {
patchFound = true;
var fileName = m.group(2).trim();
var pathSegments = fileName.split("[/\\\\]");
var segment = 0; // the segment in the path that is the project
var projectName = null;
for(var i = 0; i < pathSegments.length; i++) {
var name = pathSegments[i];
if(isProjectName(projects, name)) {
projectName = name;
segment = i;
hunkFound=true;
// report every guessed project only once
if(!guessedProjects.contains(projectName)) {
messages.append("Guessed Project: ");
messages.append(projectName);
messages.append("\n");
guessedProjects.add(projectName);
}
break;
}
}
if(projectName != null) {
result.append("#P ");
result.append(projectName);
result.append("\n");
result.append("Index: ");
// concat the remaining path segments
for(var j = segment + 1; j < pathSegments.length; j++) {
if(j != segment + 1) result.append("/");
result.append(pathSegments[j]);
}
result.append("\n");
result.append("===================================================================\n");
}
}
if(line.startsWith("+++")) {
if(!hunkFound) {
messages.append("No project found for: ");
messages.append(fileName);
messages.append("\n");
}
hunkFound = false;
}
if(prevLine != null) {
result.append(prevLine);
result.append("\n");
}
prevLine = line;
}
if(prevLine != null) {
result.append(prevLine);
result.append("\n");
}
reader.close();
if(!patchFound)
return null;
return result.toString();
}

function isProjectName(projects, name) {
// is the name a name of a project?
for(var i = 0; i < projects.length; i++) {
if(projects[i].getName().equals(name))
return true;
}
// ok let's guess...
if(name.startsWith("com.") || name.startsWith("org.")) {
return true;
}
return false;
}
function putOnClipboard(str) {
var clipboard = new dnd.Clipboard(window.getShell().getDisplay());
try {
clipboard.setContents([str], [dnd.TextTransfer.getInstance()]);
} finally {
clipboard.dispose();
}
}
function getFromClipboard() {
var textTransfer = dnd.TextTransfer.getInstance();
var clipboard = new dnd.Clipboard(window.getShell().getDisplay());
try {
return clipboard.getContents(textTransfer);
} finally {
clipboard.dispose();
}
}

--- And burbled as it ran! ---