jdk/test/java/net/NetworkInterface/MemLeakTest.java
changeset 21323 d9433f0957ae
parent 21322 10b9b204a16a
parent 21320 0a56bf0c2390
child 21324 1f9eb351241e
equal deleted inserted replaced
21322:10b9b204a16a 21323:d9433f0957ae
     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/timeout=700 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(3);
       
    58 
       
    59         long vMemBefore = getVMemSize();
       
    60         accessNetInterfaces(500_000);
       
    61         long vMemAfter = getVMemSize();
       
    62 
       
    63         long vMemDelta = vMemAfter - vMemBefore;
       
    64         if (vMemDelta > MEM_LEAK_THRESHOLD) {
       
    65             throw new Exception("FAIL: Virtual memory usage increased by " + vMemDelta + "Kb " +
       
    66                     "(greater than " + MEM_LEAK_THRESHOLD + "Kb)");
       
    67         }
       
    68 
       
    69         System.out.println("PASS: Virtual memory usage increased by " + vMemDelta + "Kb " +
       
    70                 "(not greater than " + MEM_LEAK_THRESHOLD + "Kb)");
       
    71     }
       
    72 
       
    73     private static void accessNetInterfaces(int times) {
       
    74         try {
       
    75             Collection<NetworkInterface> interfaces =
       
    76                     Collections.list(NetworkInterface.getNetworkInterfaces());
       
    77             for (int i = 0; i != times; ++i) {
       
    78                 for (NetworkInterface netInterface : interfaces) {
       
    79                     netInterface.getMTU();
       
    80                     netInterface.isLoopback();
       
    81                     netInterface.isUp();
       
    82                     netInterface.isPointToPoint();
       
    83                     netInterface.supportsMulticast();
       
    84                 }
       
    85             }
       
    86         } catch (SocketException ignore) {}
       
    87     }
       
    88 
       
    89     /**
       
    90      * Returns size of virtual memory allocated to the process in Kb.
       
    91      * Linux specific. On other platforms and in case of any errors returns 0.
       
    92      */
       
    93     private static long getVMemSize() {
       
    94 
       
    95         // Refer to the Linux proc(5) man page for details about /proc/self/stat file
       
    96         //
       
    97         // In short, this file contains status information about the current process
       
    98         // written in one line. The fields are separated with spaces.
       
    99         // The 23rd field is defined as 'vsize %lu   Virtual memory size in bytes'
       
   100 
       
   101         try (FileReader fileReader = new FileReader("/proc/self/stat");
       
   102              BufferedReader bufferedReader = new BufferedReader(fileReader)) {
       
   103             String line = bufferedReader.readLine();
       
   104             return Long.parseLong(line.split(" ")[22]) / 1024;
       
   105         } catch (Exception ignore) {}
       
   106         return 0;
       
   107     }
       
   108 }