2010年4月18日 星期日

JAVA Memory Leak

FINDING MEMORY LEAKS IN JAVA APPS

Here is a small HOWTO on how to find memory leaks with Java SE.

I’ve written it while trying to find memory leaks in our testing tools: JTHarness and ME Framework, and then wanted to share the HOWTO with the world, but I didn’t have my blog at that time, so I posted this info as a comment to a relevant entry in the excellent Adam Bien’s blog.

Note: Use the latest JDK 6, because it has the latest tools, with lots of bug fixes and improvements. All the later examples assume that JDK6’s bin directory is in the PATH.

STEP 1. START THE APPLICATION.

Start the application as you usually do:

java -jar java_app.jar

Alternatively, you could start java with hprof agent. Java will run slower, but the huge benefit of this approach is that the stack traces for created objects will be available which improves memory leak analysis greatly:

 java   -Dcom.sun.management.jmxremote   -Dcom.sun.management.jmxremote.port=9000   -Dcom.sun.management.jmxremote.authenticate=false   -Dcom.sun.management.jmxremote.ssl=false   -agentlib:hprof=heap=dump,file=/tmp/hprof.bin,    format=b,depth=10   -jar java_app.jar

When the application is up, perform various actions that you think might lead to memory leaks.

For example, if you open some documents in your app, the memory graph could rapidly go up. If closing the docs and invocation of full garbage collection did not bring the memory back to normal level, there is probably a leak somewhere.You might use jconsole from JDK 6 to see the memory consumption graph to have a clue whether memory leak is present or not:

jconsole

It will pop up a dialog with a list of Java applications to connect to. Find the one with java_app.jar and connect. Also, jconsole allows to invoke full GC providing nice button just for that.

STEP 2. FIND THE APPLICATION PID.

Find out the application’s process id via:

jps

It will print something like:

15976 java_app.jar
7586 startup.jar
22476 Jps
12248 Main
5437 Bootstrap

In our case the pid is 15976.

STEP 3. DUMP THE HEAP INTO FILE.

Dump heap into the file:

jmap -dump:format=b,file=/tmp/java_app-heap.bin 15976

We just told jmap to dump the heap into /tmp/java_app-heap.bin file, in binary from (which is optimized to work with large heaps). The third parameter is the pid we found in Step 2.

Alternatively, if you started java with hprof agent, you could just use Ctrl-\ on Solaris/Linux or Ctrl-Break on Windows to dump heap into the file, specified in hprof agent arguments.

STEP 4. VISUALIZE THE HEAP.

Use jhat tool to visualize the heap:

jhat -J-Xmx326m /tmp/java_app-heap.bin

Jhat will parse the heap dump and start a web server at port 7000. Connect to Jhat server by pointing your browser to:

http://localhost:7000

And start investigating. :)

Jhat allows you to see what objects are present in the heap, who has references to those objects, etc.

Here are some tips:

  • Investigate _instances_, not _classes_.
  • Use the following URL to see the instances: http://localhost:7000/showInstanceCounts/
  • Use “Reference Chains from Rootset” (Exclude weak refs!!!) to see who’s holding the instance.

2010年3月24日 星期三

Spring: onBindAndValidate

Well, there is a reason why the standard Validator does not offer access to the current HttpServletRequest: Validators are not web-specific in the first place, so they obviously shouldn't have any Servlet API artifacts in their signature. So I'm afraid standard Validators will remain the way they are, without access to the HttpServletRequest.

So in general, I would still recommend to override "onBindAndValidate" here. Note that you can easily delegate to some custom validator objects from there: For example, define a "RequestAwareValidator" interface yourself, implement it according to your needs, pass an instance into your controller through dependency injection, and call that instance from your "onBindAndValidate" implementation.


Refer to http://book.opensourceproject.org.cn/java/eclipse/eclipsejava/opensource/0672328968/ch07lev1sec8.html#ch07fig08

2010年3月3日 星期三

2010年2月19日 星期五

DB2: Select table sturcture

select TBNAME, NAME, COLTYPE, LENGTH, NULLS FROM SYSIBM.SYSCOLUMNS
WHERE TBCREATOR = 'schemaname' and TBNAME in ('tablename')

2010年2月10日 星期三

SVN: Eclipse Plugin

1. Help->Install New Software
2. Choose to work with Galileo – http://download.eclipse.org/releases/galileo
3. Unfold Collaboration
4. Choose Subversive SVN Team Provider (Incubation)
5. I employ you to restart Eclipse and don’t use any plugin at this point since they will not work and you will get a message which is similar to this: 0×0040010b: Obtain Project Name’ operation finished with error: Selected SVN connector library is not available or cannot be loaded …’. Instead you rather install a connector library.
6. Help->Install New Software
7. Add a site
8. I used: http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/ , but you can probably use any other that has SVNKit or JavaHL connector
9. Unfold Subversive SVN Connectors
10. Select Subversive SVN Connectors, SVNKit (1.3.0), JavaHL (1.6.0) and it’s native libraries.
11. Restart Eclipse
12. Configure SVN Connector by specifying the connector in Window->Preferences->Team->SVN->SVN Connector.

2009年10月16日 星期五

T-Mobile: WAP Setting

Services => WAP => Settings => Edit Profile => SIM1 => Pick any profile => Edit Profile
1:Rename Profile => T-MOBILE WAP
2:Home Page =>http://wap.myvoicestream.com
3:Data account :T-Mobile GPRS
4:Connection type:WAP
5:User Name:(none)
6:Password:(none)

Services => Data Account => GPRS => (chose an unused company name) =>
1:Change Account Name => T-Mobile GPRS
2:APN => wap.voicestream.com
3:User Name: (none)
4:Password:(none)
5:Auth.type: Normal


Primary DNS: 216.155.165.50

Secondary DNS: 216.155.165.51

2009年10月15日 星期四

Junit 4: Add Junit test on Ant

<target name="mytest" description="mytest">

<junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes"> <formatter type="plain" usefile="false" />

<test name="gov.hud.mfh.business.service.contracts.ContractFinderTest"/> <classpath>
<pathelement path="test"/> <!-- source code place -->
<fileset dir="test"> <!-- source lib place -->
<include name="**/*.jar"/>
<exclude name="**/ant*.jar"/>
</fileset>
<fileset dir="./lib/junit"> <!-- junit lib place -->
<include name="**/*.jar"/>
<exclude name="**/ant*.jar"/>
</fileset> </classpath>
</junit>
</target>

You must Exclude ant*.jar otherwise you will get Error:
No tests found in gov.hud.mfh.business.service.contracts
And USE ANT 1.7.0 TO RUN JUNIT 4.
ANT 1.7.1 might has problem.

Batch Junit Example

<target name="batchtest" description="batchtest">
<junit printsummary="yes" haltonerror="no" haltonfailure="yes" fork="yes">
<formatter type="xml" usefile="true" />
<classpath>
<pathelement path="test"/>
<fileset dir="test">
<include name="**/*.jar"/>
<exclude name="**/ant*.jar"/>
</fileset>
<fileset dir="./lib/junit">
<include name="**/*.jar"/>
<exclude name="**/ant*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports.junit.dir}" haltonfailure="yes" >
<fileset dir="junit">
<include name="**/*Test*.java"/>
</fileset>
<formatter type="xml"/>
</batchtest>
</junit>
</target>

Junit 4: Error on JUnit4TestAdapter signer information

class "junit.framework.JUnit4TestAdapter"'s signer information does not match signer information of other classes in the same package

Solution: more than one junit.jar appear in the classpath. Please delete the redundant one.

2009年8月16日 星期日

Create Action in ActionMenu

1. Create Files like in directory above.
2. Elements.xml:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="Simple List Actions"
GroupId="ActionsMenu"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1101"
Title="My Action"
Description="A Simple Action Menu"
ControlAssembly="CustomActions.SimpleAction, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5483709352c77bd4"
ControlClass="CustomActions.SimpleAction.SimpleActionExample"/></Elements>

3. Feature.xml

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="886DFDD5-B323-4593-9471-717C863F4CB4"
Title="Custom Simple Action Example"
Scope="Web"
Hidden="FALSE"
>
<ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>

4. SimpleActionExample.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Diagnostics;
using System.Web.UI;
using Microsoft.SharePoint;
namespace CustomActions.SimpleAction
{
public class SimpleActionExample: System.Web.UI.WebControls.WebControl
{
protected override void OnLoad(EventArgs e)
{
EnsureChildControls();
base.OnLoad(e);
}
protected override void CreateChildControls()
{
SPWeb site = SPContext.Current.Web;
MenuItemTemplate _action = new MenuItemTemplate();
_action.Text = "My Action1";
_action.Description = "My Action1";
_action.ImageUrl = "/_layouts/images/NEWITEM.GIF";
_action.ClientOnClickNavigateUrl = "http://www.ttt.com";
Controls.Add(_action);
}

7. Deploy Descripture
cd "$(ProjectDir)"xcopy "$(TargetDir)*.dll" "C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin\" /ys"
%programfiles%\microsoft visual studio 8\sdk\v2.0\bin\gacutil" /i "$(TargetPath)" /nologo /f

xcopy "TEMPLATE" "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\" /ys


6. Deploy the feature.
stsadm -o deactivatefeature -name CustomSimpleAction -url http://site/ -force
stsadm -o uninstallfeature -name CustomSimpleAction -force
stsadm -o execadmsvcjobs

stsadm.exe -o installfeature -name CustomSimpleAction
stsadm.exe -o activatefeature -name CustomSimpleAction -url http://site/ -force
stsadm -o execadmsvcjobs
iisreset

Then you found the action is not comming out......
Why????
Answer is here. You have to put it on SafeControl.

<SafeControl Assembly="CustomActions.SimpleAction, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5483709352c77bd4" Namespace="CustomActions.SimpleAction" TypeName="*" Safe="True" />

Use

<?xml version="1.0" encoding="utf-8" ?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="C467C5CE-5290-4A2B-A243-092F5A51E4E0" >
<FeatureManifests>
<FeatureManifest Location="CustomActions.SimpleAction\feature.xml" />
</FeatureManifests>
<Assemblies>
<Assembly DeploymentTarget="GlobalAssemblyCache" Location="CustomActionOCRPDFInList.dll">
<SafeControls>
<SafeControl
Assembly="CustomActions.SimpleAction, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5483709352c77bd4"
Namespace="CustomActions.SimpleAction"
TypeName="*"
Safe="True" />
</SafeControls>
</Assembly>
</Assemblies>
</Solution>

Or directly put in Web.config file

2009年8月3日 星期一

Invalid postback or callback argument

EXCEPTION:

Invalid postback or callback argument. Event validation is enabled using in configuration or in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Just make sure EnableEventValidation is set to false on web.config, otherwise the following error is being thrown: