0

我的堆转储快照低于 2 个。每次拍的时间都不一样。恐怕是我注意到对象数量的增加是正常的吗?3个对象,浅堆和reatined堆有什么区别?

快照 1

Class Name                                                       |   Objects | Shallow Heap | Retained Heap
------------------------------------------------------------------------------------------------------------
                                                                 |           |              |              
java.lang.String                                                 |    29,635 |      948,320 |  >= 2,416,080
char[]                                                           |    28,722 |    2,208,984 |  >= 2,208,984
java.lang.Object                                                 |    24,001 |      384,016 |    >= 384,016
java.util.HashMap$Entry                                          |    11,077 |      354,464 |  >= 6,204,552
java.util.TreeMap$Entry                                          |    10,316 |      412,640 |    >= 416,064
java.util.TreeMap                                                |    10,143 |      486,864 |    >= 902,960
java.util.Hashtable$Entry                                        |     7,773 |      248,736 |    >= 459,944
byte[]                                                           |     7,439 |    3,345,408 |  >= 3,345,408
java.util.HashMap$Entry[]                                        |     7,323 |      641,816 |  >= 6,329,016
java.util.HashMap                                                |     7,301 |      350,448 |  >= 6,310,048
java.lang.ref.Finalizer                                          |     5,208 |      208,320 |  >= 2,970,240
java.util.concurrent.atomic.AtomicInteger                        |     5,029 |       80,464 |     >= 80,488
java.io.FileDescriptor                                           |     5,007 |      120,168 |    >= 200,296
java.lang.Object[]                                               |     4,873 |      262,048 |    >= 845,856
java.net.SocksSocketImpl                                         |     4,857 |      543,984 |  >= 1,620,944
java.net.Socket                                                  |     4,856 |      155,392 |    >= 233,096
java.net.SocketInputStream                                       |     4,855 |      233,040 |    >= 498,160
java.net.Inet4Address                                            |     4,817 |      154,144 |    >= 203,600
com.mysql.jdbc.ConnectionPropertiesImpl$BooleanConnectionProperty|     4,400 |      281,600 |    >= 281,608
java.util.ArrayList                                              |     3,700 |       88,800 |    >= 349,928
java.util.HashSet                                                |     3,415 |       54,640 |    >= 502,096
com.mysql.jdbc.StatementImpl                                     |     3,386 |      514,672 |  >= 5,481,192
Total: 22 of 2,443 entries; 2,421 more                           |   250,175 |   14,989,928 |              
------------------------------------------------------------------------------------------------------------

快照 2

Class Name                                                       |   Objects | Shallow Heap | Retained Heap
------------------------------------------------------------------------------------------------------------
                                                                 |           |              |              
byte[]                                                           |     8,816 |    3,401,752 |  >= 3,401,752
char[]                                                           |    31,516 |    1,990,752 |  >= 1,990,752
java.lang.String                                                 |    32,595 |    1,043,040 |  >= 2,643,008
java.net.SocksSocketImpl                                         |     6,905 |      773,360 |  >= 2,311,056
java.util.HashMap$Entry[]                                        |     8,731 |      763,160 |  >= 7,448,192
com.mysql.jdbc.JDBC4ResultSet                                    |     4,068 |      715,968 |  >= 5,326,728
com.mysql.jdbc.StatementImpl                                     |     4,070 |      618,640 |  >= 6,596,656
java.util.TreeMap                                                |    12,202 |      585,696 |  >= 1,080,624
com.mysql.jdbc.Field                                             |     4,059 |      584,496 |  >= 1,071,968
java.lang.Object                                                 |    32,988 |      527,808 |    >= 527,808
java.util.TreeMap$Entry                                          |    12,346 |      493,840 |    >= 494,896
java.util.HashMap                                                |     8,709 |      418,032 |  >= 7,471,688
java.util.HashMap$Entry                                          |    12,611 |      403,552 |  >= 7,312,464
java.net.SocketInputStream                                       |     6,903 |      331,344 |    >= 710,152
java.lang.Object[]                                               |     5,592 |      302,352 |    >= 845,248
java.lang.ref.Finalizer                                          |     7,349 |      293,960 |  >= 4,300,272
com.mysql.jdbc.ConnectionPropertiesImpl$BooleanConnectionProperty|     4,400 |      281,600 |    >= 281,608
java.util.Hashtable$Entry                                        |     8,015 |      256,480 |    >= 489,904
java.net.Socket                                                  |     6,904 |      220,928 |    >= 331,400
java.net.Inet4Address                                            |     6,861 |      219,552 |    >= 289,680
java.io.FileDescriptor                                           |     7,108 |      170,592 |    >= 284,336
com.mysql.jdbc.RowDataStatic                                     |     4,056 |      129,792 |    >= 454,272
Total: 22 of 2,446 entries; 2,424 more                           |   295,977 |   16,546,032 |              
------------------------------------------------------------------------------------------------------------

第一个快照显示 14.3 mb,第二个是 15.8mb。我从运行时获取的那个显示波动。那么哪个是正确的内存使用以及如何指示任何内存泄漏?

Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
//runtime.gc();
// Calculate the used memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("\n\nUsed memory is bytes: " + memory);
//Print the jvm heap size.
long heapSize = runtime.totalMemory();
System.out.println("\n\nHeap Size = " + heapSize);

int mb = 1024*1024;
System.out.println("##### Heap utilization statistics [MB] #####");

//Print used memory
System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);

//Print free memory
System.out.println("Free Memory:" + runtime.freeMemory() / mb);

//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);

//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);
4

0 回答 0