test/java/lang/instrument/RedefineBigClassApp.java

Print this page


   1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 


  24 public class RedefineBigClassApp {
  25     public static void main(String[] args) throws Exception {
  26         System.out.println("Creating instance of " +
  27             RedefineBigClassAgent.clz);
  28         RedefineBigClassAgent.clz.newInstance();
  29 

  30         int count = 0;
  31         while (!RedefineBigClassAgent.doneRedefining) {
  32             System.out.println("App loop count: " + ++count);
  33             try {
  34                 Thread.sleep(10 * 1000);
  35             } catch (InterruptedException ie) {
  36             }
  37         }
  38         System.out.println("App looped  " + count + " times.");
  39 

















  40         System.exit(0);













  41     }
  42 }
   1 /*
   2  * Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 
  26 public class RedefineBigClassApp {
  27     public static void main(String[] args) throws Exception {
  28         System.out.println("Creating instance of " +
  29             RedefineBigClassAgent.clz);
  30         RedefineBigClassAgent.clz.newInstance();
  31 
  32         long vMemBefore = getVMemSize();
  33         int count = 0;
  34         while (!RedefineBigClassAgent.doneRedefining) {
  35             System.out.println("App loop count: " + ++count);
  36             try {
  37                 Thread.sleep(10 * 1000);
  38             } catch (InterruptedException ie) {
  39             }
  40         }
  41         System.out.println("App looped  " + count + " times.");
  42 
  43         long vMemAfter = getVMemSize();
  44         if (vMemBefore == 0 || vMemAfter == 0) {
  45             System.err.println("WARNING: Cannot perform memory leak detection on this OS");
  46         } else {
  47             long vMemDelta = vMemAfter - vMemBefore;
  48             final long MEM_LEAK_THRESHOLD = 32 * 1024; // 32Mb
  49             if (vMemDelta > MEM_LEAK_THRESHOLD) {
  50                 System.err.println("FAIL: Virtual memory usage increased by " +
  51                         Long.valueOf(vMemDelta) + "Kb " +
  52                         "(greater than " + Long.valueOf(MEM_LEAK_THRESHOLD) + "Kb)");
  53                 System.exit(1);
  54             } else {
  55                 System.err.println("PASS: Virtual memory usage increased by " +
  56                         Long.valueOf(vMemDelta) + "Kb " +
  57                         "(not greater than " + Long.valueOf(MEM_LEAK_THRESHOLD) + "Kb)");
  58             }
  59         }
  60         System.exit(0);
  61     }
  62 
  63     /**
  64      * Return size of virtual memory allocated to the process.
  65      * Linux specific. On other platforms and in case of any errors return 0.
  66      */
  67     static private long getVMemSize() {
  68         try (FileReader fileReader = new FileReader("/proc/self/stat");
  69                 BufferedReader bufferedReader = new BufferedReader(fileReader)) {
  70             String line = bufferedReader.readLine();
  71             return Long.parseLong(line.split(" ")[22]) / 1024;
  72         } catch (Exception ex) {}
  73         return 0;
  74     }
  75 }