我已按照此链接What is the difference between closing Input/OutputStream and closing Socket directly?了解了如何关闭我的Java套接字。做了一些jmap histo之后的问题我们发现套接字的数量不断增加,但结果集会波动。我的代码片段如下。
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();
//new changes added but no effect.
readBuffer = null;
writeBuffer =null;
receivedSocketConn1=null;
}
else{
System.out.println("writeBuffer is null in finally close");
}
}
catch(IOException ex){
ex.printStackTrace(System.out);
}
}
}
}
为了进一步验证我删除了我的代码,只是拥有套接字应用程序部分并删除所有其他代码。下面是任何套接字连接之前的jmap histo。
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
154: 1 32 java.net.InetAddress
160: 1 32 java.lang.NoSuchMethodError
176: 1 32 java.net.Socket
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
关闭50个套接字连接后的Jmap。只需比较一下这个java.net.Socket就可以看到加息到51。
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;
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
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
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