|
| 1 | +//package com.baeldung; |
| 2 | +// |
| 3 | +//import java.io.BufferedReader; |
| 4 | +//import java.io.File; |
| 5 | +//import java.io.IOException; |
| 6 | +//import java.io.InputStream; |
| 7 | +//import java.io.InputStreamReader; |
| 8 | +//import java.net.URISyntaxException; |
| 9 | +//import java.net.URL; |
| 10 | +//import java.net.URLConnection; |
| 11 | +//import java.nio.charset.StandardCharsets; |
| 12 | +//import java.nio.file.Files; |
| 13 | +//import java.nio.file.Paths; |
| 14 | +//import java.nio.file.StandardOpenOption; |
| 15 | +//import java.util.ArrayList; |
| 16 | +//import java.util.Map; |
| 17 | +//import java.util.Random; |
| 18 | +//import java.util.Scanner; |
| 19 | +// |
| 20 | +//import org.junit.FixMethodOrder; |
| 21 | +//import org.junit.Test; |
| 22 | +//import org.junit.runner.RunWith; |
| 23 | +//import org.junit.runners.JUnit4; |
| 24 | +//import org.junit.runners.MethodSorters; |
| 25 | +// |
| 26 | +//@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 27 | +//@RunWith(JUnit4.class) |
| 28 | +//public class MemoryLeaksTest { |
| 29 | + |
| 30 | +// private Random random = new Random(); |
| 31 | +// public static final ArrayList<Double> list = new ArrayList<Double>(1000000); |
| 32 | +// |
| 33 | +// @Test(expected = OutOfMemoryError.class) |
| 34 | +// public void givenStaticField_whenLotsOfOperations_thenMemoryLeak() throws InterruptedException { |
| 35 | +// while (true) { |
| 36 | +// int k = random.nextInt(100000); |
| 37 | +// System.out.println(k); |
| 38 | +// Thread.sleep(10000); //to allow GC do its job |
| 39 | +// for (int i = 0; i < k; i++) { |
| 40 | +// list.add(random.nextDouble()); |
| 41 | +// } |
| 42 | +// } |
| 43 | +// |
| 44 | +// } |
| 45 | +// |
| 46 | +// @SuppressWarnings({ "resource" }) |
| 47 | +// @Test(expected = OutOfMemoryError.class) |
| 48 | +// public void givenLengthString_whenIntern_thenOutOfMemory() throws IOException { |
| 49 | +// String str = new Scanner(new File("src/test/resources/large.txt"), "UTF-8").useDelimiter("\\A") |
| 50 | +// .next(); |
| 51 | +// str.intern(); |
| 52 | +// System.out.println("Done"); |
| 53 | +// } |
| 54 | + |
| 55 | +// @Test(expected = OutOfMemoryError.class) |
| 56 | +// public void givenURL_whenUnclosedStream_thenOutOfMemory() throws IOException, URISyntaxException { |
| 57 | +// String str = ""; |
| 58 | +// URLConnection conn = new URL("http://norvig.com/big.txt").openConnection(); |
| 59 | +// BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); |
| 60 | +// while (br.readLine() != null) { |
| 61 | +// str += br.readLine(); |
| 62 | +// } |
| 63 | +// Files.write(Paths.get("src/main/resources/"), str.getBytes(), StandardOpenOption.CREATE); |
| 64 | +// } |
| 65 | +// |
| 66 | +// @SuppressWarnings("unused") |
| 67 | +// @Test(expected = OutOfMemoryError.class) |
| 68 | +// public void givenConnection_whenUnclosed_thenOutOfMemory() throws IOException, URISyntaxException { |
| 69 | +// URL url = new URL("ftp://speedtest.tele2.net"); |
| 70 | +// URLConnection urlc = url.openConnection(); |
| 71 | +// InputStream is = urlc.getInputStream(); |
| 72 | +// String str = ""; |
| 73 | +// while (true) { |
| 74 | +// str += is.toString() |
| 75 | +// .intern(); |
| 76 | +// } |
| 77 | +// } |
| 78 | +// |
| 79 | +// @Test(expected = OutOfMemoryError.class) |
| 80 | +// public void givenMap_whenNoEqualsNoHashCodeMethods_thenOutOfMemory() throws IOException, URISyntaxException { |
| 81 | +// Map<Object, Object> map = System.getProperties(); |
| 82 | +// while (true) { |
| 83 | +// map.put(new Key("key"), "value"); |
| 84 | +// } |
| 85 | +// } |
| 86 | +//} |
0 commit comments