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     /**
  28      * Memory leak is assumed, if application consumes more than specified amount of memory during its execution.
  29      * The number is given in Kb.
  30      */
  31     private static final long MEM_LEAK_THRESHOLD = 32 * 1024; // 32Mb
  32 
  33     public static void main(String[] args) throws Exception {
  34         System.out.println("Creating instance of " +
  35             RedefineBigClassAgent.clz);
  36         RedefineBigClassAgent.clz.newInstance();
  37 
  38         long vMemBefore = getVMemSize();
  39         int count = 0;
  40         while (!RedefineBigClassAgent.doneRedefining) {
  41             System.out.println("App loop count: " + ++count);
  42             try {
  43                 Thread.sleep(10 * 1000);
  44             } catch (InterruptedException ie) {
  45             }
  46         }
  47         System.out.println("App looped  " + count + " times.");
  48 
  49         long vMemAfter = getVMemSize();
  50         if (vMemBefore == 0 || vMemAfter == 0) {
  51             System.err.println("WARNING: Cannot perform memory leak detection on this OS");
  52         } else {
  53             long vMemDelta = vMemAfter - vMemBefore;
  54             if (vMemDelta > MEM_LEAK_THRESHOLD) {
  55                 System.err.println("FAIL: Virtual memory usage increased by " + vMemDelta + "Kb " +
  56                         "(greater than " + MEM_LEAK_THRESHOLD + "Kb)");
  57                 System.exit(1);
  58             }
  59             System.err.println("PASS: Virtual memory usage increased by " + vMemDelta + "Kb " +
  60                     "(not greater than " + MEM_LEAK_THRESHOLD + "Kb)");
  61         }
  62         System.exit(0);
  63     }
  64 
  65     /**
  66      * Return size of virtual memory allocated to the process in Kb.
  67      * Linux specific. On other platforms and in case of any errors return 0.
  68      */
  69     private static long getVMemSize() {
  70 
  71         // Refer to the Linux proc(5) man page for details about /proc/self/stat file
  72         //
  73         // In short, this file contains status information about the current process
  74         // written in one line. The fields are separated with spaces.
  75         // The 23rd field is defined as 'vsize %lu   Virtual memory size in bytes'
  76 
  77         try (FileReader fileReader = new FileReader("/proc/self/stat");
  78              BufferedReader bufferedReader = new BufferedReader(fileReader)) {
  79             String line = bufferedReader.readLine();
  80             return Long.parseLong(line.split(" ")[22]) / 1024;
  81         } catch (Exception ex) {}
  82         return 0;
  83     }
  84 }