We have a socket application with lots of queries in it. The problem after doing some sample of jmap -histo we notice the number of sockets keep growing and resultsets fluctuates. Checked through and ensure all the resultset are closed and finally even the socket is close just based on closing the outputsream as suggested here http://stackoverflow.com/questions/3428127/what-is-the-difference-between-closing-input-outputstream-and-closing-socket-dir. Below is snippet of my codes. What worries is in my full application the socket number have reached cross 8000 and not droppping at all.

BoneCP connectionPool = null;
  class ConnectionHandler implements Runnable {


    private Socket receivedSocketConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }
    Connection dbconn = null;

    public void run() { // etc
     BufferedWriter writeBuffer = null;
     BufferedReader readBuffer = null;
     String capturedMessage="";

     try{
        dbconn = connectionPool.getConnection();
        dbconn.setAutoCommit(false);

        while ((nextChar=readBuffer.read()) != -1){          
          capturedMessage += (char) nextChar;

          if (nextChar == '*')
          {
           try{

                //all queries here.
                //for insert query this how i do
                Statement stmt1 = null;
                stmt1 = dbconn.createStatement();
                String insertQuery3 =........
                count = stmt9.executeUpdate(insertQuery3);
                try{
                  if ( stmt1!= null ){  stmt1.close();
                }
                else{
                System.out.println("No stm1 exist");

                }
                }catch(SQLException ex){   
                  System.out.println("SQLException has been caught for stmt1");
                  ex.printStackTrace(System.out);
                }

                ///For select we do this  

                Statement stmt2 = null;
                stmt2 = dbconn.createStatement(); 
                String selectQuery2= .........
                ResultSet rs2 = stmt2.executeQuery(selectQuery2);

                if(rs2.next())
                {

                }
                try{
                 if ( rs2!= null ){  
                     rs2.close();
                 }   else{
                 System.out.println("No rs2 exist");
                 }

                 if ( stmt2!= null ){ 
                      stmt2.close();
                 }   else{
                 System.out.println("No stm2 exist");

                 }
                }catch(SQLException ex)
                {   
                System.out.println("SQLException has been caught for stmt2");
                ex.printStackTrace(System.out);
                }



               dbconn.commit
            }
           catch (SQLException ex){
                ex.printStackTrace(System.out);
                try{  
              dbconn.rollback();
           }
           catch (Exception rollback){  
              rollback.printStackTrace(System.out);
              }
       }
       catch (Exception e){
           e.printStackTrace(System.out);
           try{  
              dbconn.rollback();
           }
           catch (Exception rollback){  
              rollback.printStackTrace(System.out);
             }
       }
       finally
     {

     }


        }
     catch (SocketTimeoutException ex){
           ex.printStackTrace();
     }
     catch (IOException ex){
           ex.printStackTrace();
     }
     catch (Exception ex){
           ex.printStackTrace(System.out);
     }    
      finally{
        try{
         if ( dbconn != null ){
           dbconn.close();
         }
         else{
          System.out.println("dbConn is null in finally close");
         }
        }
        catch(SQLException ex){
            ex.printStackTrace();
        }
        try{
          if ( writeBuffer != null ){
            writeBuffer.close();
         }
         else{
          System.out.println("w is null in finally close");
         }
        }
        catch(IOException ex){
            ex.printStackTrace(System.out);
        }
       }
      }
    }

This I strip down my application just to have the socket purely and capture the histo when it just activated and after 50 socket connection closed.

Jmap on active.

  num     #instances         #bytes  class name
----------------------------------------------
   1:          6023         824664  <methodKlass>
   2:          6023         726808  <constMethodKlass>
   3:           401         458376  <constantPoolKlass>
   4:           369         296320  <constantPoolCacheKlass>
   5:           401         293848  <instanceKlassKlass>
   6:           439         225264  [I
   7:          1512         165736  [C
   8:           772         144224  [B
   9:           461          56120  java.lang.Class
  10:           674          42880  [[I
  11:           602          39424  [S
  12:          1189          38048  java.lang.String
  13:            43          25112  <objArrayKlassKlass>
  14:           339          14896  [Ljava.lang.Object;
  15:             8           4672  <typeArrayKlassKlass>
  16:           112           3584  java.util.Hashtable$Entry
  17:            47           3384  java.lang.reflect.Field
  18:           132           3168  java.lang.StringBuilder
  19:            66           2352  [Ljava.lang.String;
  20:            25           2128  [Ljava.util.HashMap$Entry;
  21:            11           2112  <klassKlass>
  22:            38           1824  sun.util.locale.LocaleObjectCache$CacheEntry
  23:            28           1792  java.net.URL
  24:            74           1776  java.io.File
  25:            12           1760  [Ljava.util.Hashtable$Entry;
  26:            54           1728  java.util.concurrent.ConcurrentHashMap$HashEntry
  27:            48           1536  java.util.HashMap$Entry
  28:            36           1440  java.util.concurrent.ConcurrentHashMap$Segment
  29:            52           1408  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
  30:             3           1400  <methodDataKlass>
  31:            33           1320  java.lang.ref.Finalizer
  32:            16           1152  java.lang.reflect.Constructor
  33:            36           1152  java.util.concurrent.locks.ReentrantLock$NonfairSync
  34:            40            960  java.util.LinkedList$Node
  35:            24            960  java.util.LinkedHashMap$Entry
  36:            19            912  java.util.HashMap
  37:            20            800  sun.util.locale.BaseLocale$Key
  38:             9            720  [Ljava.util.WeakHashMap$Entry;
  39:            21            672  java.util.LinkedList
  40:            42            672  java.lang.Object
  41:            16            640  java.io.ObjectStreamField
  42:            16            640  java.lang.ref.SoftReference
  43:            32            632  [Ljava.lang.Class;
  44:             6            624  java.lang.Thread
  45:            19            608  sun.util.locale.BaseLocale
  46:            25            600  java.net.Parts
  47:            12            576  sun.misc.URLClassPath$JarLoader
  48:            22            528  sun.security.action.GetPropertyAction
  49:             6            480  java.util.jar.JarFile$JarFileEntry
  50:            20            480  java.io.ExpiringCache$Entry
  51:            20            480  java.util.Locale$LocaleKey
  52:             6            480  [Ljava.util.concurrent.ConcurrentHashMap$Segment;
  53:            19            456  java.util.Locale
  54:            11            440  java.util.WeakHashMap$Entry
  55:             9            432  sun.nio.cs.UTF_8$Encoder
  56:             9            432  java.util.WeakHashMap
  57:             6            384  java.util.jar.JarFile
  58:             6            384  java.util.zip.ZipEntry
  59:             6            384  java.nio.DirectByteBuffer
  60:            12            384  java.lang.ref.ReferenceQueue
  61:            16            384  java.lang.StringBuffer
  62:             6            336  java.util.zip.ZipFile$ZipFileInputStream
  63:             6            336  java.nio.DirectLongBufferU
  64:             6            336  java.util.zip.ZipFile$ZipFileInflaterInputStream
  65:            10            320  java.security.AccessControlContext
  66:            13            312  sun.misc.URLClassPath$3
  67:             6            288  java.util.concurrent.ConcurrentHashMap
  68:             8            256  java.util.Vector
  69:             8            256  java.lang.OutOfMemoryError
  70:             5            248  [Ljava.lang.reflect.Field;
  71:             2            224  java.net.SocksSocketImpl
  72:            14            224  java.lang.ref.ReferenceQueue$Lock
  73:             8            208  [Ljava.io.ObjectStreamField;
  74:             8            192  [Ljava.lang.reflect.Constructor;
  75:             8            192  java.lang.Class$1
  76:             8            192  sun.reflect.NativeConstructorAccessorImpl
  77:             8            192  java.util.ArrayList
  78:             4            192  java.util.Hashtable
  79:             6            192  java.util.zip.Inflater
  80:             4            192  java.nio.HeapByteBuffer
  81:             6            192  java.util.zip.ZipCoder
  82:             4            192  java.nio.HeapCharBuffer
  83:             7            168  java.io.FileDescriptor
  84:             5            160  java.io.DataInputStream
  85:             2            160  java.lang.reflect.Method
  86:             6            144  sun.misc.MetaIndex
  87:             6            144  sun.misc.FileURLMapper
  88:             6            144  java.util.zip.ZStreamRef
  89:             6            144  java.util.ArrayDeque
  90:             6            144  sun.misc.PerfCounter
  91:             8            128  sun.reflect.DelegatingConstructorAccessorImpl
  92:             8            128  java.util.concurrent.atomic.AtomicInteger
  93:             3            120  java.security.ProtectionDomain
  94:             3            120  sun.nio.cs.UTF_8$Decoder
  95:             2            112  java.util.LinkedHashMap
  96:             2            112  java.io.ExpiringCache$1
  97:             2            112  sun.nio.cs.StreamEncoder
  98:             1            104  java.lang.ref.Reference$ReferenceHandler
  99:             1            104  java.lang.ref.Finalizer$FinalizerThread
 100:             1            104  sun.net.www.protocol.file.FileURLConnection
 101:             2             96  java.lang.ThreadGroup
 102:             3             96  java.util.Stack
 103:             2             96  java.io.BufferedReader
 104:             3             96  java.security.CodeSource
 105:             2             96  java.io.BufferedWriter
 106:             2             96  sun.nio.cs.StreamDecoder
 107:             2             96  java.util.Properties
 108:             6             96  sun.misc.URLClassPath$JarLoader$1
 109:             3             96  java.io.FileInputStream
 110:             2             96  java.util.StringTokenizer
 111:             2             88  [Ljava.net.URL;
 112:             2             80  java.io.ExpiringCache
 113:             1             80  sun.misc.Launcher$ExtClassLoader
 114:             1             80  [Ljava.lang.ThreadLocal$ThreadLocalMap$Entry;
 115:             2             80  sun.misc.URLClassPath
 116:             5             80  java.lang.ThreadLocal
 117:             1             80  sun.misc.Launcher$AppClassLoader
 118:             3             72  java.util.Collections$SynchronizedSet
 119:             3             72  java.lang.RuntimePermission
 120:             3             72  sun.misc.Signal
 121:             2             64  java.io.FileOutputStream
 122:             4             64  java.lang.Class$3
 123:             2             64  java.io.FilePermission
 124:             2             64  java.lang.VirtualMachineError
 125:             2             64  java.lang.ClassLoader$NativeLibrary
 126:             2             64  [Ljava.lang.Thread;
 127:             2             64  java.lang.ref.ReferenceQueue$Null
 128:             2             64  java.lang.ThreadLocal$ThreadLocalMap$Entry
 129:             2             64  java.io.PrintStream
 130:             2             64  java.io.OutputStreamWriter
 131:             1             48  [J
 132:             2             48  java.net.InetAddress$Cache
 133:             2             48  java.net.InetAddress$Cache$Type
 134:             2             48  java.net.URLClassLoader$1
 135:             1             48  java.util.Hashtable$Enumerator
 136:             3             48  java.lang.ClassLoader$3
 137:             3             48  java.security.ProtectionDomain$Key
 138:             3             48  [Ljava.security.Principal;
 139:             3             48  java.lang.Integer
 140:             3             48  java.util.HashSet
 141:             3             48  sun.reflect.ReflectionFactory$GetReflectionFactoryAction
 142:             2             48  sun.misc.NativeSignalHandler
 143:             2             48  java.io.BufferedOutputStream
 144:             2             48  java.nio.charset.CoderResult
 145:             3             48  java.nio.charset.CodingErrorAction
 146:             2             48  [Ljava.io.File;
 147:             1             40  java.util.HashMap$EntryIterator
 148:             1             40  java.security.PrivilegedActionException
 149:             1             40  java.io.BufferedInputStream
 150:             1             40  java.lang.ClassNotFoundException
 151:             1             40  sun.nio.cs.StandardCharsets$Aliases
 152:             1             40  sun.nio.cs.StandardCharsets$Classes
 153:             1             40  sun.nio.cs.StandardCharsets$Cache
 154:             1             32  java.net.InetAddress
 155:             1             32  java.io.UnixFileSystem
 156:             2             32  sun.reflect.ReflectionFactory$1
 157:             1             32  [Ljava.lang.ThreadGroup;
 158:             1             32  sun.nio.cs.StandardCharsets
 159:             2             32  [Ljava.lang.StackTraceElement;
 160:             1             32  java.lang.NoSuchMethodError
 161:             1             32  sun.misc.URLClassPath$FileLoader$1
 162:             1             32  java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl
 163:             2             32  java.nio.ByteOrder
 164:             2             32  java.io.FilePermission$1
 165:             1             32  java.lang.StringCoding$StringEncoder
 166:             2             32  java.lang.Boolean
 167:             1             32  java.security.Permissions
 168:             1             32  java.net.Inet4Address
 169:             1             32  java.security.BasicPermissionCollection
 170:             1             32  java.lang.StringCoding$StringDecoder
 171:             2             32  sun.security.action.LoadLibraryAction
 172:             1             32  java.lang.NullPointerException
 173:             1             32  java.lang.ArithmeticException
 174:             2             32  sun.net.www.protocol.jar.Handler
 175:             1             32  [Ljava.lang.OutOfMemoryError;
 176:             1             32  java.net.Socket
 177:             1             24  sun.nio.cs.UTF_8
 178:             1             24  sun.launcher.LauncherHelper
 179:             1             24  java.util.Collections$EmptyMap
 180:             1             24  [Lsun.launcher.LauncherHelper;
 181:             1             24  sun.net.www.MessageHeader
 182:             1             24  sun.misc.URLClassPath$FileLoader
 183:             1             24  [Ljava.net.InetAddress$Cache$Type;
 184:             1             24  sun.nio.cs.US_ASCII
 185:             1             24  java.util.Collections$UnmodifiableRandomAccessList
 186:             1             24  sun.nio.cs.ISO_8859_1
 187:             1             24  java.net.Inet6AddressImpl
 188:             1             24  sun.nio.cs.UTF_16BE
 189:             1             24  sun.nio.cs.UTF_16LE
 190:             1             24  java.util.Locale$Cache
 191:             1             24  java.io.InputStreamReader
 192:             1             24  sun.nio.cs.UTF_16
 193:             1             24  java.io.FileReader
 194:             1             24  java.net.Inet4AddressImpl
 195:             1             24  java.io.FilePermissionCollection
 196:             1             24  sun.util.locale.BaseLocale$Cache
 197:             1             24  java.lang.reflect.ReflectPermission
 198:             1             24  java.lang.ThreadLocal$ThreadLocalMap
 199:             1             24  java.util.BitSet
 200:             1             24  sun.net.sdp.SdpProvider
 201:             1             24  [Ljava.lang.reflect.Method;
 202:             1             24  java.net.InetSocketAddress

Jmap after closing 50 socket connections.

 num     #instances         #bytes  class name
----------------------------------------------
   1:          7823        1070328  <methodKlass>
   2:          7823         983752  <constMethodKlass>
   3:          5717         971936  [C
   4:           536         635152  <constantPoolKlass>
   5:           495         413664  <constantPoolCacheKlass>
   6:           744         410336  [I
   7:           536         390976  <instanceKlassKlass>
   8:           711         255152  [B
   9:          2612          83584  java.lang.String
  10:           611          75240  java.lang.Class
  11:           832          58080  [S
  12:           876          54520  [[I
  13:            58          33872  <objArrayKlassKlass>
  14:          1218          29232  java.lang.StringBuilder
  15:            41          22576  <methodDataKlass>
  16:           350          17688  [Ljava.lang.Object;
  17:           269          12912  java.nio.HeapCharBuffer
  18:            41           5904  java.text.DecimalFormat
  19:            52           5824  java.net.SocksSocketImpl
  20:           310           4960  java.lang.Object
  21:             8           4672  <typeArrayKlassKlass>
  22:            40           4480  java.util.GregorianCalendar
  23:            40           3840  sun.util.calendar.Gregorian$Date
  24:            89           3832  [Ljava.lang.String;
  25:           117           3744  java.util.concurrent.ConcurrentHashMap$HashEntry
  26:           111           3552  java.util.HashMap$Entry
  27:            88           3520  java.lang.ref.Finalizer
  28:           109           3488  java.util.Hashtable$Entry
  29:             8           3200  commServer8000$ConnectionHandler
  30:            80           3200  java.util.concurrent.ConcurrentHashMap$Segment
  31:            39           3144  [Ljava.util.HashMap$Entry;
  32:            88           2816  java.lang.StackTraceElement
  33:            39           2808  java.lang.reflect.Field
  34:            41           2624  java.text.DecimalFormatSymbols
  35:            80           2560  java.util.concurrent.locks.ReentrantLock$NonfairSync
  36:            40           2560  java.text.SimpleDateFormat
  37:            50           2400  java.net.SocketInputStream
  38:            37           2368  java.net.URL
  39:            42           2352  sun.util.calendar.ZoneInfo
  40:            32           2304  java.lang.reflect.Constructor
  41:            80           2304  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
  42:             3           2120  [J
  43:            11           2112  <klassKlass>
  44:           128           2048  java.lang.Character
  45:            41           1968  sun.util.locale.LocaleObjectCache$CacheEntry
  46:            41           1640  java.text.DigitList
  47:            51           1632  java.net.Inet4Address
  48:            51           1632  java.net.Socket
  49:            40           1600  [Z
  50:            40           1600  java.util.LinkedHashMap$Entry
  51:            19           1520  [Ljava.util.concurrent.ConcurrentHashMap$Segment;
  52:            31           1488  java.util.HashMap
  53:            61           1464  java.io.FileDescriptor
  54:            14           1456  java.lang.Thread
  55:             8           1088  [Ljava.util.Hashtable$Entry;
  56:            43           1032  java.util.LinkedList$Node
  57:            63           1008  java.util.concurrent.atomic.AtomicInteger
  58:            25           1000  java.lang.ref.SoftReference
  59:            40            960  java.util.Date
  60:            19            912  java.util.concurrent.ConcurrentHashMap
  61:            18            864  java.nio.HeapByteBuffer
  62:            21            840  sun.util.locale.BaseLocale$Key
  63:            17            816  sun.nio.cs.UTF_8$Encoder
  64:            17            816  sun.misc.URLClassPath$JarLoader
  65:            32            768  java.io.ExpiringCache$Entry
  66:             9            720  [Ljava.util.WeakHashMap$Entry;
  67:            22            704  java.util.LinkedList
  68:            21            672  sun.util.locale.BaseLocale
  69:            16            640  java.io.ObjectStreamField
  70:            18            576  java.security.AccessControlContext
  71:            10            560  sun.nio.cs.StreamEncoder
  72:            10            544  [Ljava.lang.StackTraceElement;
  73:             1            528  [Ljava.lang.Character;
  74:            21            504  java.util.Locale
  75:            10            480  java.io.BufferedWriter
  76:            15            480  java.lang.ref.ReferenceQueue
  77:            19            456  java.util.Locale$LocaleKey
  78:            18            432  java.text.DateFormat$Field
  79:             9            432  java.util.WeakHashMap
  80:             5            400  java.util.jar.JarFile$JarFileEntry
  81:             7            392  java.util.ResourceBundle$CacheKey
  82:             6            384  java.util.jar.JarFile
  83:            16            384  [Ljava.lang.reflect.Constructor;
  84:            16            384  sun.reflect.NativeConstructorAccessorImpl
  85:             8            384  java.io.BufferedReader
  86:             6            384  java.nio.DirectByteBuffer
  87:             8            384  sun.nio.cs.StreamDecoder
  88:             8            384  java.net.SocketOutputStream
  89:             9            360  sun.nio.cs.UTF_8$Decoder
  90:             6            336  java.util.LinkedHashMap
  91:             6            336  java.util.zip.ZipFile$ZipFileInputStream
  92:             7            336  java.util.ResourceBundle$BundleReference
  93:             6            336  java.nio.DirectLongBufferU
  94:             6            336  java.util.zip.ZipFile$ZipFileInflaterInputStream
  95:             8            320  java.net.SocketTimeoutException
  96:            10            320  java.io.OutputStreamWriter
  97:             8            320  java.util.IdentityHashMap
  98:            13            312  java.io.File
  99:            18            296  [Ljava.lang.Class;
 100:            12            288  sun.misc.MetaIndex
 101:             6            288  java.util.Hashtable
 102:            17            272  java.lang.ref.ReferenceQueue$Lock
 103:             8            256  java.net.InetAddress
 104:             8            256  java.util.Vector
 105:            16            256  sun.reflect.DelegatingConstructorAccessorImpl
 106:             8            256  java.lang.OutOfMemoryError
 107:             5            248  [Ljava.lang.reflect.Field;
 108:             6            240  java.util.WeakHashMap$Entry
 109:             7            224  java.util.ResourceBundle$LoaderReference
 110:             7            224  java.io.FileInputStream
 111:             9            216  java.util.ArrayList
 112:             9            216  java.util.Collections$SetFromMap
 113:             8            208  [Ljava.io.ObjectStreamField;
 114:             2            192  [Ljava.text.DateFormat$Field;
 115:             8            192  java.math.RoundingMode
 116:             8            192  java.io.InputStreamReader
 117:             6            192  java.util.zip.Inflater
 118:             6            192  java.util.zip.ZipCoder
 119:             8            192  [Ljava.security.ProtectionDomain;
 120:             7            168  sun.security.action.GetPropertyAction
 121:             4            160  java.security.ProtectionDomain
 122:            10            160  java.lang.Integer
 123:             2            160  [Ljava.lang.ThreadLocal$ThreadLocalMap$Entry;
 124:             4            160  java.util.Collections$SingletonMap
 125:             6            144  java.util.zip.ZStreamRef
 126:             6            144  java.util.ArrayDeque
 127:             6            144  sun.misc.PerfCounter
 128:             4            128  java.util.Stack
 129:             4            128  java.security.CodeSource
 130:             4            128  sun.util.LocaleServiceProviderPool
 131:             8            128  java.lang.Throwable$WrappedPrintStream
 132:             8            128  java.util.IdentityHashMap$KeySet
 133:             3            120  sun.misc.URLClassPath
 134:             2            112  java.io.ExpiringCache$1
 135:             7            112  java.net.Socket$3
 136:             7            112  java.net.Socket$2
 137:             7            112  java.lang.ThreadLocal
 138:             1            104  java.lang.ref.Reference$ReferenceHandler
 139:             1            104  java.lang.ref.Finalizer$FinalizerThread
 140:             2             96  java.lang.ThreadGroup
 141:             4             96  java.util.Collections$UnmodifiableRandomAccessList
 142:             3             96  java.lang.ClassLoader$NativeLibrary
 143:             2             96  java.util.Properties
 144:             3             96  java.lang.ThreadLocal$ThreadLocalMap$Entry
 145:             2             80  java.io.ExpiringCache
 146:             2             80  java.util.Locale$Category
 147:             1             80  sun.misc.Launcher$ExtClassLoader
 148:             1             80  sun.misc.Launcher$AppClassLoader
 149:             1             80  java.lang.reflect.Method
 150:             3             72  java.util.Collections$SynchronizedSet
 151:             3             72  java.lang.RuntimePermission
 152:             3             72  sun.misc.Signal
 153:             3             72  java.util.Arrays$ArrayList
 154:             2             64  java.io.FileOutputStream
 155:             1             64  java.util.ResourceBundle$RBClassLoader
 156:             2             64  java.lang.VirtualMachineError
 157:             4             64  java.security.ProtectionDomain$Key
 158:             4             64  [Ljava.security.Principal;
 159:             2             64  [Ljava.lang.Thread;
 160:             4             64  java.util.LinkedHashSet
 161:             2             64  java.lang.ref.ReferenceQueue$Null
 162:             2             64  sun.nio.fs.UnixPath
 163:             2             64  java.io.PrintStream
 164:             2             64  sun.util.locale.UnicodeLocaleExtension
 165:             1             56  java.text.DateFormatSymbols
 166:             3             48  java.text.AttributedCharacterIterator$Attribute
 167:             2             48  java.net.InetAddress$Cache
 168:             2             48  java.net.InetAddress$Cache$Type
 169:             1             48  [Ljava.math.RoundingMode;
 170:             2             48  sun.util.locale.LocaleExtensions
 171:             3             48  java.util.HashSet
 172:             2             48  java.lang.ThreadLocal$ThreadLocalMap
 173:             2             48  sun.misc.NativeSignalHandler
 174:             3             48  sun.net.www.protocol.jar.Handler
 175:             2             48  java.io.BufferedOutputStream
 176:             2             48  java.nio.charset.CoderResult
 177:             3             48  java.nio.charset.CodingErrorAction
 178:             1             40  java.util.ResourceBundle$1
 179:             1             40  java.io.BufferedInputStream
 180:             1             40  sun.text.resources.FormatData
 181:             1             40  sun.text.resources.FormatData_en
 182:             1             40  sun.text.resources.FormatData_en_US
 183:             1             40  sun.util.resources.CurrencyNames
 184:             1             40  sun.misc.Cleaner
 185:             1             40  sun.util.resources.CurrencyNames_en_US
 186:             1             40  sun.nio.cs.StandardCharsets$Aliases
 187:             1             40  sun.util.resources.CalendarData
 188:             1             40  sun.util.resources.CalendarData_en
 189:             1             40  sun.nio.cs.StandardCharsets$Classes
 190:             1             40  sun.nio.cs.StandardCharsets$Cache
 191:             1             32  java.io.UnixFileSystem
 192:             1             32  sun.nio.fs.LinuxFileSystem
 193:             1             32  [Ljava.lang.ThreadGroup;
 194:             1             32  sun.nio.cs.StandardCharsets
 195:             1             32  sun.nio.fs.NativeBuffer
 196:             1             32  java.io.FilePermission
 197:             1             32  java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl
 198:             2             32  java.nio.ByteOrder
 199:             1             32  java.lang.StringCoding$StringEncoder
 200:             2             32  java.lang.Boolean
 201:             1             32  java.security.Permissions
 202:             1             32  java.security.BasicPermissionCollection
 203:             1             32  java.lang.StringCoding$StringDecoder
 204:             1             32  java.lang.NullPointerException
 205:             1             32  java.lang.ArithmeticException
 206:             1             32  [Ljava.lang.OutOfMemoryError;
 207:             1             24  sun.nio.cs.UTF_8
 208:             1             24  sun.launcher.LauncherHelper
 209:             1             24  java.util.Collections$EmptyMap
 210:             1             24  [Lsun.launcher.LauncherHelper;
 211:             1             24  sun.misc.URLClassPath$FileLoader
 212:             1             24  [Ljava.net.InetAddress$Cache$Type;
 213:             1             24  [Ljava.util.Locale$Category;
 214:             1             24  sun.nio.cs.US_ASCII
 215:             1             24  sun.nio.cs.ISO_8859_1
 216:             1             24  java.net.Inet6AddressImpl
 217:             1             24  sun.nio.fs.NativeBuffer$Deallocator
 218:             1             24  sun.nio.cs.UTF_16BE
 219:             1             24  sun.nio.cs.UTF_16LE
 220:             1             24  java.util.Locale$Cache
 221:             1             24  sun.nio.cs.UTF_16
 222:             1             24  java.io.FilePermissionCollection
 223:             1             24  sun.util.locale.BaseLocale$Cache
 224:             1             24  java.lang.reflect.ReflectPermission
 225:             1             24  java.util.BitSet
 226:             1             24  sun.net.sdp.SdpProvider
 227:             1             24  [Ljava.lang.reflect.Method;
 228:             1             24  sun.misc.JarIndex
 229:             1             24  java.net.ServerSocket

I don't have time to study all that data right now, but it does make me wonder if you are confusing closing the Socket with releasing the Socket object for garbage collection. Even if you close the Socket there will still be a current Socket object until the very last reference to it goes out of scope or is given a new value. Maybe you are hanging on to a reference to the Socket somewhere?
... just a thought - ignore this if it's off-target.
J

I am not sure what I am doing in a finally section this writeBuffer.close();. I dont know what else to close here?

Thats a lot of data to analyse, but after a quick look, I see you haven't closed the socket. You have closed the BufferedWriter that writes along the socket but not the socket itself. I don't know if i'm 100% correct here but it's all I could think of.

Good Luck

Dear Obsy,
          I have tried colosing it and even initialise back to null no difference.

Two important points:

  1. What is the time interval between taking the jmap dump and closing the sockets? Your close calls don't immediately take effect since there is a certain teardown time
  2. Did you also monitor the log file? Were there any exceptions in the logs?
Dear Sos,

1.There are many sockets running so I cant decide on when the exactly the socket is closed. Normally what is the time gap between for the teardown time? 
2. Yes there are times this exception "java.net.SocketTimeoutException: Read timed out" but still it should be handled by the finally section right?

I just had a Full GC I guess either yesterday or the day before. Then I compare the instances of the socket via jmap before this FGC and today which is after the FGC but the instances did not decrease but keep increasing? What could be the cause or is it wrong to refer to jmap? Another thing which is that after the full GC the Old Generation does not become 0?

How to interpret jmap or maybe I am looking at the wrong thing? I am looking at these

 48536        5436032  java.net.SocksSocketImpl
 48533        2329584  java.net.SocketInputStream
 48535        1553120  java.net.Socket

There are many sockets running so I cant decide on when the exactly the socket is closed. Normally what is the time gap between for the teardown time?

Don't use jmap for finding open sockets in your system; use an OS specific utility like netstat or something else to find open sockets for a given process. Run the command when your app starts and run it the second time after you have send across a bunch of requests. Conntinue this process and if you see monotonically increasing open socket count, there is definitely a resource leak in your code.

Yes there are times this exception "java.net.SocketTimeoutException: Read timed out" but still it should be handled by the finally section right?

The question you need to ask is "why did the read time out"? Because the answer might have a strong correlation with the sockets hanging around. Also, I don't know whether things are actually getting closed. To be honest, your code is very convoluted and complicated. I think you would have better luck simplifying it. Also, are you using Java 7? If yes, considering using the try with resources block.

Another thing which is that after the full GC the Old Generation does not become 0?

A full GC cycle doesn't clean up Old generation; it just removes unused objects. If there are still objects in the old generation which are live, they would hang around irrespective of the type of GC happening.

Dear Sos,
             I am on linux platform I do use this command too netstat -nat |grep 9000 | awk '{print $6}' | sort | uniq -c | sort -n . But this one shows me well the connection are fluctuating meaning they are closed and open. Any other method to check on them? So why jmap is not the right tool what are those instances in it then referring to?

    The read time happens because I have set this receivedSocketConn1.setSoTimeout(60000); where if there is not activity then after 1 minute the socket suppose to close. How to really confirm that the sockets are closed properly? No method to tell is it ? Yes I will post a separate question on the resultset I am also seeing those number growing in jmap too?

    So if the FGC cant clean every time  will I get a OOM later eventually which is what worries me ?

I can't do any meaningful analysis sitting here but I would recommend you approach this problem in a organized way. From your post, if seems that you have multiple issues: open sockets, timeout exceptions, Socket objects in memory and GC issues.

Tackle them one at a time. Refactor your code so that the reading records from database part is a separate method/class so that you can simplify the resource cleanup. Also from the code above, I don't see you closing readBuffer. It's also quite possible that the resource/memory leak is in some other part of the code.

Also, are you looking at the right data? You posted:

48536        5436032  java.net.SocksSocketImpl
48533        2329584  java.net.SocketInputStream
48535        1553120  java.net.Socket

whereas I see:

52 5824  java.net.SocksSocketImpl
Dear Sos,
         Ok I will listen to your advice. Ok what I post here first is my snippet codes. In that post I wrote strip down my application just to have the socket purely and capture the histo when it just activated and after 50 socket connection closed so this is 52 5824  java.net.SocksSocketImpl where its referring too. So in that version no db involved its pure socket only. 

These values are from the full running codes
48536        5436032  java.net.SocksSocketImpl
48533        2329584  java.net.SocketInputStream
48535        1553120  java.net.Socket
Dear Sos,
         Here is my strip down of my codes and changes you recommended. I close the write buffer followed by read and finall the socket itself. This is a very fundamental codes nothing in side purely socket only. 





 try{
       while ((nextChar=readBuffer.read()) != -1){          
          capturedMessage += (char) nextChar;
          if (nextChar == '*')
          {

       }

     }
     catch (SocketTimeoutException ex){
           ex.printStackTrace();
     }
     catch (IOException ex){
           ex.printStackTrace();
     }
     catch (Exception ex){
           ex.printStackTrace(System.out);
     }    
     finally{
        try{
          if ( writeBuffer != null ){
            writeBuffer.close();
            readBuffer.close();
            receivedSocketConn1.close()
         }
         else{
          System.out.println("w is null in finally close");
         }
        }
        catch(IOException ex){
            ex.printStackTrace(System.out);
        }
       }
     }



Here is the jmap upon starting my application.
 71:             2            224  java.net.SocksSocketImpl

 176:             1             32  java.net.Socket
 202:             1             24  java.net.InetSocketAddress
 206:             1             24  java.net.ServerSocket





 Here is the jmap after I make 50 connections.

   19:            52           5824  java.net.SocksSocketImpl
   37:            50           2400  java.net.SocketInputStream
   48:            51           1632  java.net.Socket
   88:             8            384  java.net.SocketOutputStream
   95:             8            320  java.net.SocketTimeoutException
   135:             7            112  java.net.Socket$3
   136:             7            112  java.net.Socket$2
   229:             1             24  java.net.ServerSocket

I believe this is the serverside dump. How are you simulating the test code? Have you writtent a multithreaded client which does this? Are you doing proper cleanup in the client code?

Also, run the following command for jmap: jmap -histo:live pid to force a GC. Also, DON'T immediately run jmap. Start up the server, take jmap, start banging it with requests, wait for 5 mins, take jmap.

Here is my client codes for the simulation.

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.*;
import java.util.concurrent.LinkedBlockingQueue;


public class stress1 extends Thread {

public static void main(String[] argv) {
    try {
        for (int i = 0; i < 50; i++) {
        Socket socket = new Socket("192.168.2.102", 8000);
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(
        socket.getInputStream()));

        out.println("$ACTEST35,30061104075130528955N10024852E000068*03A1*");
        System.out.println(in.readLine() + i);

        out.close();
        in.close();
        socket.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
   }
  }
}




My client is not multithread but my server is yes. I did run the jmap -histo:live pid. I miss the 15 lines above what I got is below.
 35:            10           1120  java.net.SocksSocketImpl
 61:             8            384  java.net.SocketInputStream
 71:             9            288  java.net.Socket

188: 1 24 java.net.ServerSocket

So basically now you don't have 50 open sockets on the server? Correct? Or do you still have a query/question?

Dear Sos,
        I run this for loop for (int i = 0; i < 50; i++) { and in it I close each socket and reopen a new one. Off course its not parallel but is in sequence. So what is your advice here where is the leakages happening? I did try to wait after 5 minutes yet the results is same just like  I take the jmap immediately after all the 50 socket.

I'm not sure I understand. Your above post shows that the jmap prints 8 lie SocketInputStream instances. Why do you say the results are the same?

Dear Sos,
        Sorry for the confusion that 8  SocketInputStream instances are after the jmap -histo:live pid not after waiting for 5 minutes. So based on these data collection what is your advice to me? 

OK, let me be a bit more explicit. The 8 instances, were they on the server? If yes, did you do the jmap after running the 50 connection test i.e. were they after running the test or before?

Dear Sos,
         If immedately after the test or I wait 5 minutes this my result   37:            50           2400  java.net.SocketInputStream and only when I do jmap live I got that 8 instances.

That's because non-live objects tend to stick around till a GC cycle. Running jmap with histo:live option forces a GC and hence you see around 8 instances. Now you just need to make the .close call related changes your existing code and try out the entire thing again.

Dear Sos,
         How to decide what is non-live objects? So you guess this is normal is it? But why do all the rest of the socket instances for instances like this one is from my real live system is keeep growing. These are big number I guess and this is after a FULL GC. Before FULL GC the number was smaller then isnt the full GC suppose to clear like jmap with histo:live 



       48536        5436032  java.net.SocksSocketImpl
    48533        2329584  java.net.SocketInputStream
    48535        1553120  java.net.Socket

How to decide what is non-live objects?

You don't need to decide, that's why you run jmap with histo:live so that non-live objects are not shown.

But why do all the rest of the socket instances for instances like this one is from my real live system is keeep growing

Because you still haven't made the .close call related changes. I would recommend close inputstream, outputstream and sockets in your actual code, redeploying and restarting your application and then collecting the numbers again.

Dear Sos,
         I ran few tests today. First I ran with all the inputstream,outstream and then sockets closed.



 On start of the socket 

90:            25           2800  java.net.SocksSocketImpl
138:            23           1104  java.net.SocketInputStream
139:            22           1056  java.net.SocketOutputStream
170:            24            768  java.net.Socket
220:            20            480  java.net.InetSocketAddress
255:            19            304  java.net.Socket$2
256:            19            304  java.net.Socket$3
257:            19            304  java.net.SocksSocketImpl$3
529:             1             24  java.net.ServerSocket

Immediately after the test
53:            77           8624  java.net.SocksSocketImpl
82:            75           3600  java.net.SocketInputStream
100:            76           2432  java.net.Socket
113:            37           1776  java.net.SocketOutputStream
211:            10            400  java.net.SocketTimeoutException
282:             9            144  java.net.Socket$2
283:             9            144  java.net.Socket$3
480:             1             24  java.net.ServerSocket

 5 Minutes after the test
53:            77           8624  java.net.SocksSocketImpl
82:            75           3600  java.net.SocketInputStream
100:            76           2432  java.net.Socket
113:            37           1776  java.net.SocketOutputStream
211:            10            400  java.net.SocketTimeoutException
282:             9            144  java.net.Socket$2
283:             9            144  java.net.Socket$3
480:             1             24  java.net.ServerSocket


 Using jmap histo:live
42:            53           5936  java.net.SocksSocketImpl
70:            51           2448  java.net.SocketInputStream
80:            52           1664  java.net.Socket
93:            24           1152  java.net.SocketOutputStream
428:             1             24  java.net.ServerSocket




Second results I just closed the outputsream only. You will notice the outpustream close is showing better results with the jmap histo:live any reason for this?

On start of the socket 

90:            25           2800  java.net.SocksSocketImpl
138:           23           1104  java.net.SocketInputStream
139:           22           1056  java.net.SocketOutputStream
220:            20            480  java.net.InetSocketAddress
255:            19            304  java.net.Socket$2
256:            19            304  java.net.Socket$3
257:            19            304  java.net.SocksSocketImpl$3

Immediately after the test
3:            77           8624  java.net.SocksSocketImpl
82:           75           3600  java.net.SocketInputStream
99:           76           2432  java.net.Socket
113:          37           1776  java.net.SocketOutputStream
211:          10           400  java.net.SocketTimeoutException
282:          9            144  java.net.Socket$2
283:          9            144  java.net.Socket$3
480:          1             24  java.net.ServerSocket


5 Minutes after the test
53:            77           8624  java.net.SocksSocketImpl
82:            75           3600  java.net.SocketInputStream
82:            75           3600  java.net.SocketInputStream
99:            76           2432  java.net.Socket
113:           37           1776  java.net.SocketOutputStream
211:           10            400  java.net.SocketTimeoutException
282:            9            144  java.net.Socket$2
283:            9            144  java.net.Socket$3
480:            1             24  java.net.ServerSocket


Using jmap histo:live
62:            26           2912  java.net.SocksSocketImpl
89:            24           1152  java.net.SocketInputStream
90:            24           1152  java.net.SocketOutputStream
114:           25            800  java.net.Socket
428:            1             24  java.net.ServerSocket
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.