1 /*
   2  * Copyright (c) 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 /* @test
  25  * @bug 8022584
  26  * @summary Some NetworkInterface methods can leak native memory
  27  * @run main/othervm MemLeakTest
  28  */
  29 
  30 /* Note: the test can cause a memory leak that's why othervm option is required
  31  */
  32 
  33 import java.io.BufferedReader;
  34 import java.io.FileReader;
  35 import java.net.NetworkInterface;
  36 import java.net.SocketException;
  37 import java.util.Collection;
  38 import java.util.Collections;
  39 
  40 public class MemLeakTest {
  41 
  42     /**
  43      * Memory leak is assumed, if application consumes more than specified amount of memory during its execution.
  44      * The number is given in Kb.
  45      */
  46     private static final long MEM_LEAK_THRESHOLD = 32 * 1024; // 32Mb
  47 
  48     public static void main(String[] args)
  49             throws Exception {
  50 
  51         if (!System.getProperty("os.name").equals("Linux")) {
  52             System.out.println("Test only runs on Linux");
  53             return;
  54         }
  55 
  56         // warm up
  57         accessNetInterfaces();
  58         accessNetInterfaces();
  59         accessNetInterfaces();
  60 
  61         long vMemBefore = getVMemSize();
  62         for (int i = 0; i < 320000; ++i) {
  63             accessNetInterfaces();
  64         }
  65         long vMemAfter = getVMemSize();
  66 
  67         long vMemDelta = vMemAfter - vMemBefore;
  68         if (vMemDelta > MEM_LEAK_THRESHOLD) {
  69             throw new Exception("FAIL: Virtual memory usage increased by " + vMemDelta + "Kb " +
  70                     "(greater than " + MEM_LEAK_THRESHOLD + "Kb)");
  71         }
  72 
  73         System.out.println("PASS: Virtual memory usage increased by " + vMemDelta + "Kb " +
  74                 "(not greater than " + MEM_LEAK_THRESHOLD + "Kb)");
  75     }
  76 
  77     private static void accessNetInterfaces() {
  78         try {
  79             Collection<NetworkInterface> interfaces =
  80                     Collections.list(NetworkInterface.getNetworkInterfaces());
  81             for (NetworkInterface netInterface : interfaces) {
  82                 netInterface.getMTU();
  83                 netInterface.isLoopback();
  84                 netInterface.isUp();
  85                 netInterface.isPointToPoint();
  86                 netInterface.supportsMulticast();
  87             }
  88         } catch (SocketException ignore) {}
  89     }
  90 
  91     /**
  92      * Returns size of virtual memory allocated to the process in Kb.
  93      * Linux specific. On other platforms and in case of any errors return 0.
  94      */
  95     private static long getVMemSize() {
  96 
  97         // Refer to the Linux proc(5) man page for details about /proc/self/stat file
  98         //
  99         // In short, this file contains status information about the current process
 100         // written in one line. The fields are separated with spaces.
 101         // The 23rd field is defined as 'vsize %lu   Virtual memory size in bytes'
 102 
 103         try (FileReader fileReader = new FileReader("/proc/self/stat");
 104              BufferedReader bufferedReader = new BufferedReader(fileReader)) {
 105             String line = bufferedReader.readLine();
 106             return Long.parseLong(line.split(" ")[22]) / 1024;
 107         } catch (Exception ignore) {}
 108         return 0;
 109     }
 110 }