Archive for March, 2010

Reflecting outer class

Given an inner class object, how do you use reflection to obtain a reference to the outer class? this$0.

  Field parent = inner.getClass().getDeclaredField("this$0");
  Object outer = parent.get(inner);

If it is a nested inner class, increment the 0.


Track memory usage in QTP with WMI

I used QTP to simulate a repeating user scenario in order to trace a memory leak with YourKit over time. So I’m using this quick & dirty function to poll the application regularly about it’s raw memory usage.

Function GetMemory
	pid = GetWindow().GetROProperty("process id")
	Set wmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set resultset = wmiService.ExecQuery("SELECT WorkingSetSize FROM Win32_Process WHERE Handle=" & pid)
	For Each result In resultset
		GetMemory = Int(result.WorkingSetSize)
End Function

GetWindow() returns a reference to the application window.

It seems even though I SELECT only WorkingSetSize I could get other information such as Handle from the result.

The For Each loop was an easy way out since I didn’t know how to access the first element.