Monday, October 28, 2013

Programmatically gettting Eclipse installation path directory

You might have some properties file or some other file at your eclipse home directory and you want to read that, for this you required a eclipse home directory path.

Below is the piece of code which helps you to get that.

public class EclipseLocation {

@SuppressWarnings("unchecked")
public static <T> T getService(Class<T> clazz, String filter) {
BundleContext context = MyPlugin.getDefault().getBundle().getBundleContext();
ServiceTracker tracker = null;
try {
tracker = new ServiceTracker(context, context.createFilter("(&(" + Constants.OBJECTCLASS + "=" + clazz.getName() //$NON-NLS-1$ //$NON-NLS-2$
+ ")" + filter + ")"), null); //$NON-NLS-1$ //$NON-NLS-2$
tracker.open();
return (T) tracker.getService();
} catch (InvalidSyntaxException e) {
return null;
} finally {
if (tracker != null)
tracker.close();
}
}
}

Invocation:
EclipseLocation.getService(Location.class, Location.INSTALL_FILTER)


Output:
C:/myInstallations/Eclipse



You can also achieve the same thing using the system property 'eclipse.launcher'.

//this returns C:/myInstallations/Eclipse/eclipse.exe
String launcherLocation = System.getProperty("eclipse.launcher");  //

if (launcherLocation != null ) {
File launcherLocationFile = new File(launcherLocation);

//get the parent of eclipse.exe file->C:/myInstallations/Eclipse
String eclipseDir = launcherLocationFile.getParent();

//your piece of logical code resides here
                   }



Hope this helps.

Thursday, October 24, 2013

Reading Windows registry values using java


This can be done using JNA(Java Native Access) library.

Use this link to download @ https://github.com/twall/jna#readme

Required jars: 

Piece of code to test:

Here I am trying to read Android installation path from windows registry.


public static void main(String[] args) {

try {
Map<String, Object> values = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Android SDK Tools");
for (String value : values.keySet()) {
System.out.println(value + "   " + values.get(value));

}
} catch (Exception e) {
e.printStackTrace();
}

}

public static void main(String[] args) {

try {
Map<String, Object> values = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Android SDK Tools");
for (String value : values.keySet()) {
System.out.println(value + "   " + values.get(value));

}
} catch (Exception e) {
e.printStackTrace();
}

}


Output:
Path   D:\Work\Android\adt-bundle-windows-x86-20130522\adt-bundle-windows-x86-20130522

Friday, October 18, 2013

Java VM PermGen space

The Java VM memory is split up to 3 memory spaces:

  • The Java Heap. Applicable for all JVM vendors, usually split between YoungGen (nursery) & OldGen (tenured) spaces.
  • The PermGen (permanent generation). Applicable to the Sun HotSpot VM only (PermGen space will be removed in future Java 7 or Java 8 updates)
  • The Native Heap (C-Heap). Applicable for all JVM vendors.

The Java HotSpot VM permanent generation space is the JVM storage used mainly to store your Java Class objects.  The Java Heap is the primary storage that is storing the actual short and long term instances of your PermGen Class objects.

The PermGen space is fairly static by nature unless using third party tool and/or Java Reflection API which relies heavily on dynamic class loading.
It is important to note that this memory storage is applicable only for a Java HotSpot VM; other JVM vendors such as IBM and Oracle JRockit do not have such fixed and configurable PermGen storage and are using other techniques to manage the non Java Heap memory (native memory).

Below diagram showcases various heap memories and how we can configure them using vm arguments.




Understanding how the JVM uses native memory on Windows

JVM: Permgen is a non-heap


In Oracle's JVM, the permanent generation is not part of the heap. It's a separate space for class definitions and related data. In Java 6 and earlier, interned strings were also stored in the permanent generation. In Java 7, interned strings are stored in the main object heap.


From above link, we can understand these terms:

Eden Space (heap): The pool from which memory is initially allocated for most objects.

Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.

Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.

Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.

Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.



Finding java process id's

Go to command prompt and type jps, this will provide the currently running java process id's in your system.




Troubleshooting Memory Leaks

http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/memleaks.html


Meaning of OutOfMemoryError

One common indication of a memory leak is the java.lang.OutOfMemoryError error. This error is thrown when there is insufficient space to allocate an object in the Java heap or in a particular area of the heap. The garbage collector cannot make any further space available to accommodate a new object, and the heap cannot be expanded further.
When the java.lang.OutOfMemoryError error is thrown, a stack trace is printed also.
java.lang.OutOfMemoryError can also be thrown by native library code when a native allocation cannot be satisfied, for example, if swap space is low.
An early step to diagnose an OutOfMemoryError is to determine what the error means. Does it mean that the Java heap is full, or does it mean that the native heap is full? To help you answer this question, the following subsections explain some of the possible error messages, with reference to the detail part of the message: