Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GC和YGC的发生和下一步行动_Java_Memory Management_Garbage Collection - Fatal编程技术网

Java GC和YGC的发生和下一步行动

Java GC和YGC的发生和下一步行动,java,memory-management,garbage-collection,Java,Memory Management,Garbage Collection,我们有一个java应用程序,我可以说它也使用了很多字符串变量。此外,我们在启动时将最大内存和初始内存设置为256Mb。此后,它运行了一段时间,直到最近,我们发现,首先是一个GC,近2周后,又是一个GC。因此,我们发现FC的平均值为0.2145s,YGC为0.0511s。我们不确定这些值是否合适或是否会导致其他结果?我们决定将内存增加到512Mb,并研究其他一些GC设置,如CMS设置?还应该优化代码,例如字符串变量的使用?通常,什么类型的对象或变量会移动到旧对象中 Timestamp S0

我们有一个java应用程序,我可以说它也使用了很多字符串变量。此外,我们在启动时将最大内存和初始内存设置为256Mb。此后,它运行了一段时间,直到最近,我们发现,首先是一个GC,近2周后,又是一个GC。因此,我们发现FC的平均值为0.2145s,YGC为0.0511s。我们不确定这些值是否合适或是否会导致其他结果?我们决定将内存增加到512Mb,并研究其他一些GC设置,如CMS设置?还应该优化代码,例如字符串变量的使用?通常,什么类型的对象或变量会移动到旧对象中

Timestamp     S0      S1     E     O      P      YGC    YGCT      FGC FGCT   GCT
   2752472.6  69.74   0.00   3.67  51.72  44.17   4614  235.630   2   0.429  236.059
   2752477.6  69.74   0.00   5.38  51.72  44.17   4614  235.630   2   0.429  236.059
   2752482.6  69.74   0.00   7.51  51.72  44.17   4614  235.630   2   0.429  236.059
   2752487.6  69.74   0.00   9.52  51.72  44.17   4614  235.630   2   0.429  236.059
   2752492.6  69.74   0.00  10.55  51.72  44.17   4614  235.630   2   0.429  236.059
代码片段

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);
        }
       }
      }
    }
来自的终结器分析 sun.misc.Launcher$ExtClassLoader@0xe003a648(50%) 大小:11.2MB类:196个对象:201.7k类加载器:7

总共有91个对象实现了finalize方法

 Histogram of Objects with Finalize Method
Class Name                         Objects Shallow Heap 
com.mysql.jdbc.JDBC4Connection
First 10 of 38 objects             38      42,560 
java.net.SocksSocketImpl
First 10 of 39 objects             39      4,368 
java.util.jar.JarFile
All 6 objects                      6        384 
java.util.zip.Inflater
All 6 objects                      6        192 
java.util.concurrent.ScheduledThreadPoolExecutor
All 1 objects                      1        80 
java.util.concurrent.ThreadPoolExecutor
All 1 objects                      1        72 
Total: 6 entries
 91 47,656 
Class Name                        Objects Shallow Heap 
java.net.SocksSocketImpl
First 10 of 18,354 objects        18,354  2,055,648 
com.sun.mail.smtp.SMTPTransport
First 10 of 162 objects           162     25,920 
java.io.FileOutputStream
First 10 of 742 objects           742     23,744 
com.mysql.jdbc.JDBC4Connection
First 10 of 15 objects            15       16,800 
java.util.jar.JarFile
First 10 of 20 objects            20       1,280 
java.util.zip.Inflater
First 10 of 25 objects            25       800 
java.util.concurrent.ThreadPoolExecutor
All 8 objects                     8        576 
sun.net.www.protocol.jar.URLJarFile
All 5 objects                     5        400 
java.util.concurrent.ScheduledThreadPoolExecutor
All 2 objects                     2        160 
java.lang.ClassLoader$NativeLibrary
All 4 objects                     4        128 
java.io.FileInputStream
All 3 objects                     3        96 
sun.jdbc.odbc.JdbcOdbcDriver
All 1 objects                     1        24 
Total: 12 entries
 19,341 2,125,576 
 Histogram of Objects with Finalize Method
Class Name                              Objects Shallow Heap 
java.util.jar.JarFile
First 10 of 11 objects                  11      704 
java.util.zip.Inflater
All 10 objects                          10      320 
java.util.concurrent.ThreadPoolExecutor
All 1 objects                           1       72 
Total: 3 entries
 22 1,096 
来自的终结器分析

(46%) 大小:13.6MB类:578个对象:304.8k类加载器:4

总共有19341个对象实现了finalize方法

 Histogram of Objects with Finalize Method
Class Name                         Objects Shallow Heap 
com.mysql.jdbc.JDBC4Connection
First 10 of 38 objects             38      42,560 
java.net.SocksSocketImpl
First 10 of 39 objects             39      4,368 
java.util.jar.JarFile
All 6 objects                      6        384 
java.util.zip.Inflater
All 6 objects                      6        192 
java.util.concurrent.ScheduledThreadPoolExecutor
All 1 objects                      1        80 
java.util.concurrent.ThreadPoolExecutor
All 1 objects                      1        72 
Total: 6 entries
 91 47,656 
Class Name                        Objects Shallow Heap 
java.net.SocksSocketImpl
First 10 of 18,354 objects        18,354  2,055,648 
com.sun.mail.smtp.SMTPTransport
First 10 of 162 objects           162     25,920 
java.io.FileOutputStream
First 10 of 742 objects           742     23,744 
com.mysql.jdbc.JDBC4Connection
First 10 of 15 objects            15       16,800 
java.util.jar.JarFile
First 10 of 20 objects            20       1,280 
java.util.zip.Inflater
First 10 of 25 objects            25       800 
java.util.concurrent.ThreadPoolExecutor
All 8 objects                     8        576 
sun.net.www.protocol.jar.URLJarFile
All 5 objects                     5        400 
java.util.concurrent.ScheduledThreadPoolExecutor
All 2 objects                     2        160 
java.lang.ClassLoader$NativeLibrary
All 4 objects                     4        128 
java.io.FileInputStream
All 3 objects                     3        96 
sun.jdbc.odbc.JdbcOdbcDriver
All 1 objects                     1        24 
Total: 12 entries
 19,341 2,125,576 
 Histogram of Objects with Finalize Method
Class Name                              Objects Shallow Heap 
java.util.jar.JarFile
First 10 of 11 objects                  11      704 
java.util.zip.Inflater
All 10 objects                          10      320 
java.util.concurrent.ThreadPoolExecutor
All 1 objects                           1       72 
Total: 3 entries
 22 1,096 
来自org.rzo.yajsw.boot.WrapperClassLoader@0xe02818c0(4%)的终结器分析 大小:966.3KB类:361个对象:19.9k类装入器:3

总共有22个对象实现finalize方法

 Histogram of Objects with Finalize Method
Class Name                         Objects Shallow Heap 
com.mysql.jdbc.JDBC4Connection
First 10 of 38 objects             38      42,560 
java.net.SocksSocketImpl
First 10 of 39 objects             39      4,368 
java.util.jar.JarFile
All 6 objects                      6        384 
java.util.zip.Inflater
All 6 objects                      6        192 
java.util.concurrent.ScheduledThreadPoolExecutor
All 1 objects                      1        80 
java.util.concurrent.ThreadPoolExecutor
All 1 objects                      1        72 
Total: 6 entries
 91 47,656 
Class Name                        Objects Shallow Heap 
java.net.SocksSocketImpl
First 10 of 18,354 objects        18,354  2,055,648 
com.sun.mail.smtp.SMTPTransport
First 10 of 162 objects           162     25,920 
java.io.FileOutputStream
First 10 of 742 objects           742     23,744 
com.mysql.jdbc.JDBC4Connection
First 10 of 15 objects            15       16,800 
java.util.jar.JarFile
First 10 of 20 objects            20       1,280 
java.util.zip.Inflater
First 10 of 25 objects            25       800 
java.util.concurrent.ThreadPoolExecutor
All 8 objects                     8        576 
sun.net.www.protocol.jar.URLJarFile
All 5 objects                     5        400 
java.util.concurrent.ScheduledThreadPoolExecutor
All 2 objects                     2        160 
java.lang.ClassLoader$NativeLibrary
All 4 objects                     4        128 
java.io.FileInputStream
All 3 objects                     3        96 
sun.jdbc.odbc.JdbcOdbcDriver
All 1 objects                     1        24 
Total: 12 entries
 19,341 2,125,576 
 Histogram of Objects with Finalize Method
Class Name                              Objects Shallow Heap 
java.util.jar.JarFile
First 10 of 11 objects                  11      704 
java.util.zip.Inflater
All 10 objects                          10      320 
java.util.concurrent.ThreadPoolExecutor
All 1 objects                           1       72 
Total: 3 entries
 22 1,096 
历史直播结果

 num     #instances         #bytes  class name
----------------------------------------------
   1:          6001       18078232  [I
   2:         44562        7196072  [B
   3:        103183        6054240  [C
   4:         31832        4786936  <constMethodKlass>
   5:         31832        4340240  <methodKlass>
   6:         31638        3543456  java.net.SocksSocketImpl
   7:         20127        3542352  com.mysql.jdbc.JDBC4ResultSet
   8:         41022        3461576  [Ljava.util.HashMap$Entry;
   9:        104185        3333920  java.lang.String
  10:         20198        3070096  com.mysql.jdbc.StatementImpl
  11:         20323        2926512  com.mysql.jdbc.Field
  12:         60329        2895792  java.util.TreeMap
  13:          2343        2800032  <constantPoolKlass>
  14:         61239        2449560  java.util.TreeMap$Entry
  15:        149690        2395040  java.lang.Object
  16:         41000        1968000  java.util.HashMap
  17:          2343        1895080  <instanceKlassKlass>
  18:          1954        1569536  <constantPoolCacheKlass>
  19:         31623        1517904  java.net.SocketInputStream
运行50个插座后

 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

512MB听起来没那么多,可能不值得你花时间进一步优化它。顺便说一句,512MB是512兆位

中长期存活的对象最终会出现在老一代

您可以使用内存分析器优化代码,以减少字符串的使用。您可以使用StringBuilder回收对象并降低GC压力,这是导致对象错误地进入永久空间的常见原因


如果将其发挥到极致,您可能会发现您可以消除所有字符串生成,但我怀疑这是否值得,将内存增加到1GB可能更简单/更便宜。

中期对象的寿命足够长,可以到达永久空间,然后死亡。长期目标在旧世代持续很长时间。StringBuilder是一个选项,但我看不出有什么需要,因为您只需要多一点内存。我使用你的工具包,但我会先试用VisualVM,因为它是免费的,内置的。你可以用不到500英镑的价格买到64 GB,也就是说每GB大约12美元。节省几美元的内存(当然,假设这是您或您的客户的成本)值得付出多少努力。CMS可以通过同时清理来避免填充旧的gen。如果CMS不能跟上您产生垃圾的速度,即在您填充旧一代之前,它没有清理足够的垃圾,则会发生完全GC。CMS使用更多的CPU,但如果不是这样,您可能会有空闲的CPU。如果您有一台所有CPU大部分时间都很忙的机器,那么使用并行收集器更好。Perm gen不容易清理,但这只在卸载类装入器时发生。您经常这样做吗?perm gen在加载和编译新方法时会增加。小幅度的增长也是正常的。CMS可以再增加10%的CPU,但在你的事业中,我怀疑它会更低。如果您不必担心类装入器,那么就不必担心perm gen。