You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Oleg Kalnichevski (JIRA)" <ji...@apache.org> on 2007/10/01 14:39:51 UTC

[jira] Updated: (HTTPCORE-118) Out of memory when opening and closing lots of connections in a tight loop

     [ https://issues.apache.org/jira/browse/HTTPCORE-118?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oleg Kalnichevski updated HTTPCORE-118:
---------------------------------------

      Component/s:     (was: Examples)
                       (was: HttpCore)
         Priority: Minor  (was: Critical)
    Fix Version/s:     (was: 4.0-alpha5)
                   4.0-alpha6
         Assignee: Oleg Kalnichevski
          Summary: Out of memory when opening and closing lots of connections in a tight loop  (was: Some strange issue under a heavy  stress test)

All right. I reviewed I/O reactor classes for possible memory and was unable to find anything that might lead to memory leaks, particularly when opening and closing lots of connections. I found one thing, though. Presently I/O reactors process new connections before purging closed sessions. So, at least theoretically, I/O reactor may run out of memory if it has lots of new sessions to open and lots of closed sessions to purge. 

I am going to change the sequence of operations in the I/O selector loop: handle I/O event first, then detect timed out sessions, then purge closed sessions, register new channels with the selector and create new sessions last. I can't think of anything else. More code reviews / stress tests are very welcome.

Oleg

> Out of memory when opening and closing lots of connections in a tight loop
> --------------------------------------------------------------------------
>
>                 Key: HTTPCORE-118
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-118
>             Project: HttpComponents Core
>          Issue Type: Bug
>          Components: HttpCore NIO
>    Affects Versions: 4.0-alpha5
>         Environment: Windows xp(sp2), JDK 1.5
>            Reporter: Halo chen
>            Assignee: Oleg Kalnichevski
>            Priority: Minor
>             Fix For: 4.0-alpha6
>
>         Attachments: a.txt, SendToIA.java, SendToIA.java, snapshot.jpg
>
>
> Thanks very much for your contributions.
> I use your httpcore to develop a embed web application, very well, I think. 
> But, when i do some stress test, i find a strange issue, please see my test case:
> public class SendToIA implements Runnable {
> 	String[] args;
> 	SendToIA( String[] args )
> 	{
> 		this.args = args;
> 	}
> 	public void run()
> 	{
> 		try {
> 			FileInputStream fis = new FileInputStream("a.txt");
> 			int len = fis.available();
> 			byte[] buf = new byte[len];
> 			fis.read(buf);
> 			fis.close();
> 			String ReuestHead = "POST /TerminalLocationNotification HTTP/1.1\r\nHOST: LOCALHOST\r\nContent-Length: ";
> 			String ReuestLen = String.valueOf(len) + "\r\n\r\n";
> 			String RequestBody = new String(buf);
> 			String httpRequest = ReuestHead + ReuestLen + RequestBody;			
> 			String host = "localhost";
> 			int port = 8086;
> 			if( args.length == 1 )
> 			{
> 				host = args[0];
> 			}
> 			else if( args.length == 2 )
> 			{
> 				host = args[0];
> 				port = Integer.parseInt( args[ 1 ] );
> 			}
> 			
> 			System.out.println(new java.util.Date());
> 			for (int i = 0; i < 20000; i++) {
> 				Socket connection = new Socket( host, port );
> 				connection.setReuseAddress( true );
> 				PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
> 				out.print(httpRequest);
> 				out.flush();
> 				connection.close();		
> 				System.out.print("\b\rcont:"+i);
> 			}
> 			System.out.println(new java.util.Date());
> 		} catch (SecurityException e) {
> 			e.printStackTrace();
> 		} catch (Exception e) {
> 			e.printStackTrace();
> 		}
> 	}
> 	public static void main(String[] args) throws IOException {
> 		int threadcount = 1;
> 		if( args.length == 3 )
> 		{
> 			threadcount = Integer.parseInt( args[ 2 ] );
> 		}
> 		Thread[] t = new Thread[ threadcount ];
> 		for( int i = 0 ; i < threadcount ; i ++ )
> 		{
> 			new Thread( new SendToIA( args )	).start();
> 		}
> 		for( int i = 0 ; i < threadcount ; i ++ )
> 		{
> 			try{t[i].join();}catch( Exception e ){}
> 		}
> 	}
> }
> Unfortunately, the application was crashed. 
> The first , I think my application has some mistake. so I run the example( org.apache.http.examples.nio.NHttpServer) in the HttpCore binary package. Oh,No!
> The same issue was occured! 
> Why? I found the memory usage of the application reach 170M!!, and then I add the JVM parameter -Xmx256M.Okay, the application can running continue. but after a long time, the memory usage of the application reach 256M and then the same thing was occured! So , I think that the HttpCore has some memory leak.
> Can you give me some advices to resolve this issue? or I have some mistakes?
> details of error occured:
> ...
> Exception in thread "I/O reactor worker thread 2" java.lang.OutOfMemoryError: Java heap space
> ....
> java.lang.NullPointerException
> 	at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.verifyWorkers(AbstractMultiworkerIOReactor.java:136)
> 	at org.apache.http.impl.nio.reactor.DefaultListeningIOReactor.execute(DefaultListeningIOReactor.java:111)
> ....
> Exception in thread "I/O reactor worker thread 8" java.lang.OutOfMemoryError: Java heap space
> Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space
> Sorry , I'm chinese , my english is bad, may be i haven't explain the issue clearly. but I need your help.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpcomponents-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpcomponents-dev-help@jakarta.apache.org