forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create memory-leaks module * Memory Leaks * Added str.intern() * Memory Leaks updated
- Loading branch information
Showing
4 changed files
with
5,527 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>memory-leaks</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources/</directory> | ||
<excludes> | ||
<exclude>**/*.java</exclude> | ||
</excludes> | ||
</resource> | ||
<resource> | ||
<directory>src/test/resources/</directory> | ||
</resource> | ||
</resources> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.baeldung; | ||
|
||
public class Key { | ||
|
||
public static String key; | ||
|
||
public Key(String key) { | ||
Key.key = key; | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
guest/memory-leaks/src/test/java/com/baeldung/MemoryLeaksTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//package com.baeldung; | ||
// | ||
//import java.io.BufferedReader; | ||
//import java.io.File; | ||
//import java.io.IOException; | ||
//import java.io.InputStream; | ||
//import java.io.InputStreamReader; | ||
//import java.net.URISyntaxException; | ||
//import java.net.URL; | ||
//import java.net.URLConnection; | ||
//import java.nio.charset.StandardCharsets; | ||
//import java.nio.file.Files; | ||
//import java.nio.file.Paths; | ||
//import java.nio.file.StandardOpenOption; | ||
//import java.util.ArrayList; | ||
//import java.util.Map; | ||
//import java.util.Random; | ||
//import java.util.Scanner; | ||
// | ||
//import org.junit.FixMethodOrder; | ||
//import org.junit.Test; | ||
//import org.junit.runner.RunWith; | ||
//import org.junit.runners.JUnit4; | ||
//import org.junit.runners.MethodSorters; | ||
// | ||
//@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
//@RunWith(JUnit4.class) | ||
//public class MemoryLeaksTest { | ||
|
||
// private Random random = new Random(); | ||
// public static final ArrayList<Double> list = new ArrayList<Double>(1000000); | ||
// | ||
// @Test(expected = OutOfMemoryError.class) | ||
// public void givenStaticField_whenLotsOfOperations_thenMemoryLeak() throws InterruptedException { | ||
// while (true) { | ||
// int k = random.nextInt(100000); | ||
// System.out.println(k); | ||
// Thread.sleep(10000); //to allow GC do its job | ||
// for (int i = 0; i < k; i++) { | ||
// list.add(random.nextDouble()); | ||
// } | ||
// } | ||
// | ||
// } | ||
// | ||
// @SuppressWarnings({ "resource" }) | ||
// @Test(expected = OutOfMemoryError.class) | ||
// public void givenLengthString_whenIntern_thenOutOfMemory() throws IOException { | ||
// String str = new Scanner(new File("src/test/resources/large.txt"), "UTF-8").useDelimiter("\\A") | ||
// .next(); | ||
// str.intern(); | ||
// System.out.println("Done"); | ||
// } | ||
|
||
// @Test(expected = OutOfMemoryError.class) | ||
// public void givenURL_whenUnclosedStream_thenOutOfMemory() throws IOException, URISyntaxException { | ||
// String str = ""; | ||
// URLConnection conn = new URL("http://norvig.com/big.txt").openConnection(); | ||
// BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); | ||
// while (br.readLine() != null) { | ||
// str += br.readLine(); | ||
// } | ||
// Files.write(Paths.get("src/main/resources/"), str.getBytes(), StandardOpenOption.CREATE); | ||
// } | ||
// | ||
// @SuppressWarnings("unused") | ||
// @Test(expected = OutOfMemoryError.class) | ||
// public void givenConnection_whenUnclosed_thenOutOfMemory() throws IOException, URISyntaxException { | ||
// URL url = new URL("ftp://speedtest.tele2.net"); | ||
// URLConnection urlc = url.openConnection(); | ||
// InputStream is = urlc.getInputStream(); | ||
// String str = ""; | ||
// while (true) { | ||
// str += is.toString() | ||
// .intern(); | ||
// } | ||
// } | ||
// | ||
// @Test(expected = OutOfMemoryError.class) | ||
// public void givenMap_whenNoEqualsNoHashCodeMethods_thenOutOfMemory() throws IOException, URISyntaxException { | ||
// Map<Object, Object> map = System.getProperties(); | ||
// while (true) { | ||
// map.put(new Key("key"), "value"); | ||
// } | ||
// } | ||
//} |
Oops, something went wrong.