non-heap memory area spring leak in JVM 1.50_6
Please let me question though it doesn't understand be good from this topic.
A problem occurs when testing as follows.
The problem is to generate memory leak in non-heap area.
%java CconnectTest2 129.24.34.68 130.104.10.50 6101 5000 185000 100
(%java -server CconnectTest2 source-ipaddress destination-ipaddress src-port dst-port thread-count)
CconnectTest2.java
--
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.channels.IllegalBlockingModeException;
import java.util.Calendar;
public class CconnectTest2 {
/**
* @param bind_ip connect_ip connect_port wait_cntconnect_TIMEOUT thread_cnt
*
*/
public static void main(String[] args) {
if ((args[0].equals("")) || (args[0]==null)) {
System.err.println("bind_ip*");
return ;
}
if ((args[1].equals("")) || (args[1]==null)) {
System.err.println("connect_ip*");
return ;
}
if ((args[2].equals("")) || (args[2]==null)) {
System.err.println("connect_port*");
return ;
}
if ((args[3].equals("")) || (args[3]==null)) {
System.err.println("wait_cnt*");
return ;
}
if ((args[4].equals("")) || (args[4]==null)) {
System.err.println("connect_TIMEOUT*");
return ;
}
if ((args[5].equals("")) || (args[5]==null)) {
System.err.println("thread_cnt*");
return ;
}
String bind_ip=args[0];
String connect_ip=args[1];
int connect_port=Integer.parseInt(args[2]);
int wait_cnt=Integer.parseInt(args[3]);
int connect_TIMEOUT=Integer.parseInt(args[4]);
int thread_cnt=Integer.parseInt(args[5]);
System.out.println("bind_ip="+bind_ip);
System.out.println("connect_ip="+connect_ip);
System.out.println("connect_port="+connect_port);
System.out.println("wait_cnt="+wait_cnt);
System.out.println("connect_TIMEOUT="+connect_TIMEOUT);
System.out.println("thread_cnt="+thread_cnt);
int i=0;
while( true ){
i++;
if( i<= thread_cnt){
ConnectExe a = new ConnectExe(connect_ip,bind_ip,connect_port,connect_TIMEOUT,wait_cnt) ;
a.start() ;
try {
Thread.sleep( 1000 ) ;
} catch (InterruptedException ex) {
}
System.out.println("thread_start[ "+i+"]");
} else {
System.out.println("thread_start OK"+thread_cnt);
break ;
}
}
while( true ){
System.gc() ;
try {
Thread.sleep( 20000 ) ;
} catch (InterruptedException ex) {
}
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) ;
int min = Calendar.getInstance().get(Calendar.MINUTE) ;
int sec = Calendar.getInstance().get(Calendar.SECOND) ;
Runtime run = Runtime.getRuntime();
long freesize = 0 ;
freesize = (run.maxMemory()-run.totalMemory())+run.freeMemory() ;
if( freesize > 0 ){
freesize = freesize/1024 ;
}
System.out.println( "" + hour + ":" + min + ":" + sec
+ " HeepFreeSize:"+ freesize + " K"
+ " activeThread:" + Thread.activeCount() ) ;
}
}
static public class ConnectExe extends Thread{
String serverAddress=null;
String bindip=null;
intport=6100;
inttimeout=0;
intwait_cnt=0;
public ConnectExe(String ne,String bind,int connect_port,int timeout,int wait_cnt ){
this.serverAddress = ne ;
this.bindip = bind ;
this.port = connect_port ;
this.timeout= timeout;
this.wait_cnt= wait_cnt;
}
public void run(){
while( true ){
SocketAddress socketAddress = null;
Socket socket = null;
try {
socket = new Socket();
socket.setKeepAlive(true);
byte[] ip = InetAddress.getByName(this.serverAddress).getAddress();
InetAddress addr = InetAddress.getByAddress(this.serverAddress, ip);
if (bindip == null) {
socketAddress = new InetSocketAddress(addr, this.port);
} else {
socketAddress = new InetSocketAddress(addr, this.port);
SocketAddress bindAddress = new InetSocketAddress(this.bindip, socket.getPort());
socket.bind(bindAddress);
}
System.out.println( "connect:" + this.serverAddress ) ;
socket.connect(socketAddress, this.timeout);
socket.close();
System.out.println( "close :" + this.serverAddress ) ;
socket = null;
} catch (UnknownHostException e) {
System.out.println("This socket cannot be connected with the server(UnknownHostException) " + e.getMessage() ) ;
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
System.out.println( e1.getMessage() ) ;
}
socket = null;
socketAddress = null;
}
} catch (SocketTimeoutException e) {
System.out.println("This socket cannot be connected with the server (SocketTimeoutException) " + e.getMessage() ) ;
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
System.out.println( e1.getMessage() ) ;
}
socket = null;
socketAddress = null;
}
} catch (IllegalBlockingModeException e) {
System.out.println("This socket cannot be connected with the server (IllegalBlockingModeException) " + e.getMessage() ) ;
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
System.out.println( e1.getMessage() ) ;
}
socket = null;
socketAddress = null;
}
} catch (IllegalArgumentException e) {
System.out.println("This socket cannot be connected with the server (IllegalArgumentException) " + e.getMessage() ) ;
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
System.out.println( e1.getMessage() ) ;
}
socket = null;
socketAddress = null;
}
} catch (IOException e) {
System.out.println("This socket cannot be connected with the server (IOException) " + e.getMessage() ) ;
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
System.out.println( e1.getMessage() ) ;
}
socket = null;
socketAddress = null;
}
} catch (Exception e) {
System.out.println("This socket cannot be connected with the server (Exception) " + e.getMessage() ) ;
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
System.out.println( e1.getMessage() ) ;
}
socket = null;
socketAddress = null;
}
}
try {
Thread.sleep( this.wait_cnt ) ;
} catch (InterruptedException e) {
}
}
}
}
}
--
It leaks gradually when executing it for a long time.
It makes the generation of the memory leak disregarding timeout error of the
connection a problem.
Is this a bug of JVM?
Or, is it a problem of coding?
Environment:
JVM Sun JVM 1.5.0_6
OS RedHat Enterprise Linux ES 3 update6 (kernel 2.4.21-37ELsmp)

