Skip to content

Commit ac8e04b

Browse files
maibinEugen
authored and
Eugen
committedJun 30, 2017
Updated MemoryLeaks article (eugenp#2184)
* Create memory-leaks module * Memory Leaks * Added str.intern() * Memory Leaks updated * Updated guest article * Memory Leaks update * Remove unused resource
1 parent 3184c49 commit ac8e04b

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed
 

‎guest/memory-leaks/src/test/java/com/baeldung/MemoryLeaksTest.java

+21-12
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,30 @@
2626
//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
2727
//@RunWith(JUnit4.class)
2828
//public class MemoryLeaksTest {
29-
3029
// private Random random = new Random();
3130
// public static final ArrayList<Double> list = new ArrayList<Double>(1000000);
3231
//
33-
// @Test(expected = OutOfMemoryError.class)
32+
// @Test
3433
// 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-
// }
34+
// for (int i = 0; i < 1000000; i++) {
35+
// list.add(random.nextDouble());
4236
// }
37+
// Thread.sleep(10000); //to allow GC do its job
38+
// }
4339
//
40+
//
41+
// @Test
42+
// public void givenNormalField_whenLotsOfOperations_thenGCWorksFine() throws InterruptedException {
43+
// addElementsToTheList();
44+
// System.gc();
45+
// Thread.sleep(10000); //to allow GC do its job
46+
// }
47+
//
48+
// private void addElementsToTheList(){
49+
// ArrayList<Double> list = new ArrayList<Double>(1000000);
50+
// for (int i = 0; i < 1000000; i++) {
51+
// list.add(random.nextDouble());
52+
// }
4453
// }
4554
//
4655
// @SuppressWarnings({ "resource" })
@@ -51,11 +60,11 @@
5160
// str.intern();
5261
// System.out.println("Done");
5362
// }
54-
63+
//
5564
// @Test(expected = OutOfMemoryError.class)
5665
// public void givenURL_whenUnclosedStream_thenOutOfMemory() throws IOException, URISyntaxException {
5766
// String str = "";
58-
// URLConnection conn = new URL("http://norvig.com/big.txt").openConnection();
67+
// URLConnection conn = new URL("http:norvig.com/big.txt").openConnection();
5968
// BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
6069
// while (br.readLine() != null) {
6170
// str += br.readLine();
@@ -66,7 +75,7 @@
6675
// @SuppressWarnings("unused")
6776
// @Test(expected = OutOfMemoryError.class)
6877
// public void givenConnection_whenUnclosed_thenOutOfMemory() throws IOException, URISyntaxException {
69-
// URL url = new URL("ftp://speedtest.tele2.net");
78+
// URL url = new URL("ftp:speedtest.tele2.net");
7079
// URLConnection urlc = url.openConnection();
7180
// InputStream is = urlc.getInputStream();
7281
// String str = "";

‎xml/src/test/resources/Customer1.xml

100755100644
File mode changed.

0 commit comments

Comments
 (0)
Please sign in to comment.