Skip to content

Commit 317ed15

Browse files
ramansahasiKevinGilmore
authored andcommitted
BAEL-2015 Memory leaks in Java (eugenp#5295)
* BAEL-2015_Memory_leaks_in_Java * Ignored tests * Update NonStaticFieldsDemo.java * Corrected class name * converted tabspaces to whitespaces
1 parent b4aa279 commit 317ed15

19 files changed

Lines changed: 410 additions & 0 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.memoryleaks.equalshashcode;
2+
3+
public class Person {
4+
public String name;
5+
6+
public Person(String name) {
7+
this.name = name;
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.memoryleaks.equalshashcode;
2+
3+
public class PersonOptimized {
4+
public String name;
5+
6+
public PersonOptimized(String name) {
7+
this.name = name;
8+
}
9+
10+
@Override
11+
public boolean equals(Object o) {
12+
if (o == this) return true;
13+
if (!(o instanceof PersonOptimized)) {
14+
return false;
15+
}
16+
PersonOptimized person = (PersonOptimized) o;
17+
return person.name.equals(name);
18+
}
19+
20+
@Override
21+
public int hashCode() {
22+
int result = 17;
23+
result = 31 * result + name.hashCode();
24+
return result;
25+
}}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.memoryleaks.finalize;
2+
3+
import java.nio.charset.Charset;
4+
import java.util.Random;
5+
6+
public class BulkyObject {
7+
private String data[];
8+
9+
public BulkyObject() {
10+
data = new String[1000000];
11+
12+
for(int i=0; i<1000000; i++) {
13+
data[i] = getRandomString();
14+
}
15+
}
16+
17+
private String getRandomString() {
18+
byte[] array = new byte[1000];
19+
new Random().nextBytes(array);
20+
return new String(array, Charset.forName("UTF-8"));
21+
}
22+
23+
@Override
24+
public void finalize() {
25+
try {
26+
Thread.sleep(100);
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
System.out.println("Finalizer called");
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.memoryleaks.finalize;
2+
3+
import java.nio.charset.Charset;
4+
import java.util.Random;
5+
6+
public class BulkyObjectOptimized {
7+
private String data[];
8+
9+
public BulkyObjectOptimized() {
10+
data = new String[1000000];
11+
12+
for(int i=0; i<1000000; i++) {
13+
data[i] = getRandomString();
14+
}
15+
}
16+
17+
private String getRandomString() {
18+
byte[] array = new byte[1000];
19+
new Random().nextBytes(array);
20+
return new String(array, Charset.forName("UTF-8"));
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.memoryleaks.innerclass;
2+
3+
import java.nio.charset.Charset;
4+
import java.util.Random;
5+
6+
public class BulkyObject {
7+
private String data[];
8+
9+
public BulkyObject() {
10+
data = new String[1000000];
11+
12+
for(int i=0; i<1000000; i++) {
13+
data[i] = getRandomString();
14+
}
15+
}
16+
17+
private String getRandomString() {
18+
byte[] array = new byte[1000];
19+
new Random().nextBytes(array);
20+
return new String(array, Charset.forName("UTF-8"));
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.memoryleaks.innerclass;
2+
3+
public class InnerClassDriver {
4+
public static InnerClassWrapper.SimpleInnerClass getSimpleInnerClassObj() {
5+
return new InnerClassWrapper().new SimpleInnerClass();
6+
}
7+
8+
public static void main2(String[] args) {
9+
InnerClassWrapper.SimpleInnerClass simpleInnerClassObj = getSimpleInnerClassObj();
10+
System.out.println("Debug point");
11+
}
12+
13+
public static void main(String[] args) {
14+
StaticNestedClassWrapper.StaticNestedClass simpleInnerClassObj = new StaticNestedClassWrapper.StaticNestedClass();
15+
System.out.println("Debug point");
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.memoryleaks.innerclass;
2+
3+
4+
public class InnerClassWrapper {
5+
private BulkyObject bulkyObject = new BulkyObject();
6+
7+
public class SimpleInnerClass {
8+
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.memoryleaks.innerclass;
2+
3+
4+
public class StaticNestedClassWrapper {
5+
private BulkyObject bulkyObject = new BulkyObject();
6+
7+
public static class StaticNestedClass {
8+
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.memoryleaks.internedstrings;
2+
3+
public class InternedString {
4+
private static final String FILEPATH = "C:\\bigstring.txt";
5+
6+
public void readString() {
7+
String s1 = ReadStringFromFileUtil.read(FILEPATH).intern();
8+
String s2 = ReadStringFromFileUtil.read(FILEPATH).intern();
9+
10+
if (s1 == s2) {
11+
System.out.println("Both the strings objects are same");
12+
}
13+
else {
14+
System.out.println("Both the strings objects are different");
15+
}
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.memoryleaks.internedstrings;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
public class ReadStringFromFileUtil {
8+
9+
public static String read(String fileName) {
10+
BufferedReader br = null;
11+
try {
12+
br = new BufferedReader(new FileReader(fileName));
13+
StringBuilder sb = new StringBuilder();
14+
String line = br.readLine();
15+
16+
while (line != null) {
17+
sb.append(line);
18+
sb.append("\n");
19+
line = br.readLine();
20+
}
21+
return sb.toString();
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
} finally {
25+
try {
26+
br.close();
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
return null;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)