Skip to content

Commit 4f5ccb9

Browse files
maibinEugen
authored andcommitted
Memory Leaks article (eugenp#2142)
* Create memory-leaks module * Memory Leaks * Added str.intern() * Memory Leaks updated
1 parent 7ee3e57 commit 4f5ccb9

4 files changed

Lines changed: 5527 additions & 0 deletions

File tree

‎guest/memory-leaks/pom.xml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>memory-leaks</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>junit</groupId>
11+
<artifactId>junit</artifactId>
12+
<version>4.12</version>
13+
</dependency>
14+
</dependencies>
15+
16+
<build>
17+
<resources>
18+
<resource>
19+
<directory>src/main/resources/</directory>
20+
<excludes>
21+
<exclude>**/*.java</exclude>
22+
</excludes>
23+
</resource>
24+
<resource>
25+
<directory>src/test/resources/</directory>
26+
</resource>
27+
</resources>
28+
<plugins>
29+
<plugin>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<version>3.5</version>
32+
<configuration>
33+
<source>1.8</source>
34+
<target>1.8</target>
35+
</configuration>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung;
2+
3+
public class Key {
4+
5+
public static String key;
6+
7+
public Key(String key) {
8+
Key.key = key;
9+
}
10+
11+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)