Updated MemoryLeaks article (#2184)

* Create memory-leaks module

* Memory Leaks

* Added str.intern()

* Memory Leaks updated

* Updated guest article

* Memory Leaks update

* Remove unused resource
This commit is contained in:
maibin 2017-06-30 23:29:50 +02:00 committed by Eugen
parent 3184c49da5
commit ac8e04b651
2 changed files with 21 additions and 12 deletions

View File

@ -26,21 +26,30 @@
//@FixMethodOrder(MethodSorters.NAME_ASCENDING) //@FixMethodOrder(MethodSorters.NAME_ASCENDING)
//@RunWith(JUnit4.class) //@RunWith(JUnit4.class)
//public class MemoryLeaksTest { //public class MemoryLeaksTest {
// private Random random = new Random(); // private Random random = new Random();
// public static final ArrayList<Double> list = new ArrayList<Double>(1000000); // public static final ArrayList<Double> list = new ArrayList<Double>(1000000);
// //
// @Test(expected = OutOfMemoryError.class) // @Test
// public void givenStaticField_whenLotsOfOperations_thenMemoryLeak() throws InterruptedException { // public void givenStaticField_whenLotsOfOperations_thenMemoryLeak() throws InterruptedException {
// while (true) { // for (int i = 0; i < 1000000; i++) {
// int k = random.nextInt(100000); // list.add(random.nextDouble());
// System.out.println(k);
// Thread.sleep(10000); //to allow GC do its job
// for (int i = 0; i < k; i++) {
// list.add(random.nextDouble());
// }
// } // }
// Thread.sleep(10000); //to allow GC do its job
// }
// //
//
// @Test
// public void givenNormalField_whenLotsOfOperations_thenGCWorksFine() throws InterruptedException {
// addElementsToTheList();
// System.gc();
// Thread.sleep(10000); //to allow GC do its job
// }
//
// private void addElementsToTheList(){
// ArrayList<Double> list = new ArrayList<Double>(1000000);
// for (int i = 0; i < 1000000; i++) {
// list.add(random.nextDouble());
// }
// } // }
// //
// @SuppressWarnings({ "resource" }) // @SuppressWarnings({ "resource" })
@ -51,11 +60,11 @@
// str.intern(); // str.intern();
// System.out.println("Done"); // System.out.println("Done");
// } // }
//
// @Test(expected = OutOfMemoryError.class) // @Test(expected = OutOfMemoryError.class)
// public void givenURL_whenUnclosedStream_thenOutOfMemory() throws IOException, URISyntaxException { // public void givenURL_whenUnclosedStream_thenOutOfMemory() throws IOException, URISyntaxException {
// String str = ""; // String str = "";
// URLConnection conn = new URL("http://norvig.com/big.txt").openConnection(); // URLConnection conn = new URL("http:norvig.com/big.txt").openConnection();
// BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); // BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
// while (br.readLine() != null) { // while (br.readLine() != null) {
// str += br.readLine(); // str += br.readLine();
@ -66,7 +75,7 @@
// @SuppressWarnings("unused") // @SuppressWarnings("unused")
// @Test(expected = OutOfMemoryError.class) // @Test(expected = OutOfMemoryError.class)
// public void givenConnection_whenUnclosed_thenOutOfMemory() throws IOException, URISyntaxException { // public void givenConnection_whenUnclosed_thenOutOfMemory() throws IOException, URISyntaxException {
// URL url = new URL("ftp://speedtest.tele2.net"); // URL url = new URL("ftp:speedtest.tele2.net");
// URLConnection urlc = url.openConnection(); // URLConnection urlc = url.openConnection();
// InputStream is = urlc.getInputStream(); // InputStream is = urlc.getInputStream();
// String str = ""; // String str = "";

0
xml/src/test/resources/Customer1.xml Executable file → Normal file
View File