[BAEL-9555] - Created a core-java-modules folder
This commit is contained in:
9
core-java-modules/core-java-perf/README.md
Normal file
9
core-java-modules/core-java-perf/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
## Core Java Performance
|
||||
|
||||
### Relevant Articles:
|
||||
- [Verbose Garbage Collection in Java](https://www.baeldung.com/java-verbose-gc)
|
||||
- [Different Ways to Capture Java Heap Dumps](https://www.baeldung.com/java-heap-dump-capture)
|
||||
- [Understanding Memory Leaks in Java](https://www.baeldung.com/java-memory-leaks)
|
||||
- [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded)
|
||||
- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions)
|
||||
- [Monitoring Java Applications with Flight Recorder](https://www.baeldung.com/java-flight-recorder-monitoring)
|
||||
33
core-java-modules/core-java-perf/pom.xml
Normal file
33
core-java-modules/core-java-perf/pom.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<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>core-java-perf</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-perf</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.flightrecorder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Simple program that illustrates how to use Java Flight Recorder.
|
||||
*
|
||||
* This programs creates a list, inserts objects in it until
|
||||
* an OutOfMemoryError is thrown.
|
||||
*
|
||||
*/
|
||||
public class FlightRecorder {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Object> items = new ArrayList<>(1);
|
||||
try {
|
||||
while (true) {
|
||||
items.add(new Object());
|
||||
}
|
||||
} catch (OutOfMemoryError e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
assert items.size() > 0;
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.gc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A simple Java program to demonstrate how to enable verbose Garbage Collection (GC) logging.
|
||||
* <p>
|
||||
* This simple program loads 3 million {@link java.lang.String} instances into a {@link java.util.HashMap}
|
||||
* object before making an explicit call to the garbage collector using <code>System.gc()</code>.
|
||||
* <p>
|
||||
* Finally, it removes 2 million of the {@link java.lang.String} instances from the {@link java.util.HashMap}.
|
||||
* We also explicitly use <code>System.out.println</code> to make interpreting the output easier.
|
||||
* <p>
|
||||
* Run this program with the following arguments to see verbose GC logging in its complete form:
|
||||
* <pre>
|
||||
* -XX:+UseSerialGC -Xms1024m -Xmx1024m -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:/path/to/file/gc.log
|
||||
* </pre>
|
||||
* Where:
|
||||
* <ul>
|
||||
* <li><code>-XX:+UseSerialGC</code> - specify the serial garbage collector.</li>
|
||||
* <li><code>-Xms1024m</code> - specify the minimal heap size.</li>
|
||||
* <li><code>-Xmx1024m</code> - specify the maximal heap size.</li>
|
||||
* <li><code>-verbose:gc</code> - activate the logging of garbage collection information in its simplest form.</li>
|
||||
* <li><code>-XX:+PrintGCDetails</code> - activate detailed information about garbage collection.</li>
|
||||
* <li><code>-XX:+PrintGCTimeStamps</code> - include a timestamp in the output reflecting the real-time passed in seconds since the JVM started.</li>
|
||||
* <li><code>-XX:+PrintGCDateStamps</code> - include the absolute date and time at the start of each line.</li>
|
||||
* <li><code>-Xloggc</code> - by default the GC log is written to stdout. Specify an output file via this argument.</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* It should be noted that the first three arguments are not strictly necessary but for the purposes or the example
|
||||
* help really simplify things.
|
||||
*
|
||||
*/
|
||||
public class VerboseGarbageCollectorRunner {
|
||||
|
||||
private static Map<String, String> stringContainer = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Start of program!");
|
||||
String stringWithPrefix = "stringWithPrefix";
|
||||
|
||||
// Load Java Heap with 3 M java.lang.String instances
|
||||
for (int i = 0; i < 3000000; i++) {
|
||||
String newString = stringWithPrefix + i;
|
||||
stringContainer.put(newString, newString);
|
||||
}
|
||||
System.out.println("MAP size: " + stringContainer.size());
|
||||
|
||||
// Explicit GC!
|
||||
System.gc();
|
||||
|
||||
// Remove 2 M out of 3 M
|
||||
for (int i = 0; i < 2000000; i++) {
|
||||
String newString = stringWithPrefix + i;
|
||||
stringContainer.remove(newString);
|
||||
}
|
||||
|
||||
System.out.println("MAP size: " + stringContainer.size());
|
||||
System.out.println("End of program!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.heapdump;
|
||||
|
||||
import com.sun.management.HotSpotDiagnosticMXBean;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class HeapDump {
|
||||
|
||||
public static void dumpHeap(String filePath, boolean live) throws IOException {
|
||||
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||
HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(
|
||||
server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
|
||||
mxBean.dumpHeap(filePath, live);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String file = Paths.get("dump.hprof").toFile().getPath();
|
||||
|
||||
dumpHeap(file, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.jmx;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Game implements GameMBean {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Game.class);
|
||||
|
||||
private String playerName;
|
||||
|
||||
@Override
|
||||
public void playFootball(String clubName) {
|
||||
LOG.debug(this.playerName + " playing football for " + clubName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPlayerName() {
|
||||
LOG.debug("Return playerName " + this.playerName);
|
||||
return playerName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerName(String playerName) {
|
||||
LOG.debug("Set playerName to value " + playerName);
|
||||
this.playerName = playerName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.jmx;
|
||||
|
||||
public interface GameMBean {
|
||||
|
||||
public void playFootball(String clubName);
|
||||
|
||||
public String getPlayerName();
|
||||
|
||||
public void setPlayerName(String playerName);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.jmx;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.management.*;
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
public class JMXTutorialMainlauncher {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JMXTutorialMainlauncher.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
LOG.debug("This is basic JMX tutorial");
|
||||
ObjectName objectName = null;
|
||||
try {
|
||||
objectName = new ObjectName("com.baeldung.tutorial:type=basic,name=game");
|
||||
} catch (MalformedObjectNameException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||
Game gameObj = new Game();
|
||||
try {
|
||||
server.registerMBean(gameObj, objectName);
|
||||
} catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOG.debug("Registration for Game mbean with the platform server is successfull");
|
||||
LOG.debug("Please open jconsole to access Game mbean");
|
||||
while (true) {
|
||||
// to ensure application does not terminate
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.memoryleaks.equalshashcode;
|
||||
|
||||
public class Person {
|
||||
public String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.memoryleaks.equalshashcode;
|
||||
|
||||
public class PersonOptimized {
|
||||
public String name;
|
||||
|
||||
public PersonOptimized(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof PersonOptimized)) {
|
||||
return false;
|
||||
}
|
||||
PersonOptimized person = (PersonOptimized) o;
|
||||
return person.name.equals(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result = 31 * result + name.hashCode();
|
||||
return result;
|
||||
}}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.memoryleaks.finalize;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class BulkyObject {
|
||||
private String data[];
|
||||
|
||||
public BulkyObject() {
|
||||
data = new String[1000000];
|
||||
|
||||
for(int i=0; i<1000000; i++) {
|
||||
data[i] = getRandomString();
|
||||
}
|
||||
}
|
||||
|
||||
private String getRandomString() {
|
||||
byte[] array = new byte[1000];
|
||||
new Random().nextBytes(array);
|
||||
return new String(array, Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finalize() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Finalizer called");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.memoryleaks.finalize;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class BulkyObjectOptimized {
|
||||
private String data[];
|
||||
|
||||
public BulkyObjectOptimized() {
|
||||
data = new String[1000000];
|
||||
|
||||
for(int i=0; i<1000000; i++) {
|
||||
data[i] = getRandomString();
|
||||
}
|
||||
}
|
||||
|
||||
private String getRandomString() {
|
||||
byte[] array = new byte[1000];
|
||||
new Random().nextBytes(array);
|
||||
return new String(array, Charset.forName("UTF-8"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class BulkyObject {
|
||||
private String data[];
|
||||
|
||||
public BulkyObject() {
|
||||
data = new String[1000000];
|
||||
|
||||
for(int i=0; i<1000000; i++) {
|
||||
data[i] = getRandomString();
|
||||
}
|
||||
}
|
||||
|
||||
private String getRandomString() {
|
||||
byte[] array = new byte[1000];
|
||||
new Random().nextBytes(array);
|
||||
return new String(array, Charset.forName("UTF-8"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
public class InnerClassDriver {
|
||||
public static InnerClassWrapper.SimpleInnerClass getSimpleInnerClassObj() {
|
||||
return new InnerClassWrapper().new SimpleInnerClass();
|
||||
}
|
||||
|
||||
public static void main2(String[] args) {
|
||||
InnerClassWrapper.SimpleInnerClass simpleInnerClassObj = getSimpleInnerClassObj();
|
||||
System.out.println("Debug point");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
StaticNestedClassWrapper.StaticNestedClass simpleInnerClassObj = new StaticNestedClassWrapper.StaticNestedClass();
|
||||
System.out.println("Debug point");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
|
||||
public class InnerClassWrapper {
|
||||
private BulkyObject bulkyObject = new BulkyObject();
|
||||
|
||||
public class SimpleInnerClass {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
|
||||
public class StaticNestedClassWrapper {
|
||||
private BulkyObject bulkyObject = new BulkyObject();
|
||||
|
||||
public static class StaticNestedClass {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.memoryleaks.internedstrings;
|
||||
|
||||
public class InternedString {
|
||||
private static final String FILEPATH = "C:\\bigstring.txt";
|
||||
|
||||
public void readString() {
|
||||
String s1 = ReadStringFromFileUtil.read(FILEPATH).intern();
|
||||
String s2 = ReadStringFromFileUtil.read(FILEPATH).intern();
|
||||
|
||||
if (s1 == s2) {
|
||||
System.out.println("Both the strings objects are same");
|
||||
}
|
||||
else {
|
||||
System.out.println("Both the strings objects are different");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.memoryleaks.internedstrings;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReadStringFromFileUtil {
|
||||
|
||||
public static String read(String fileName) {
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
br = new BufferedReader(new FileReader(fileName));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = br.readLine();
|
||||
|
||||
while (line != null) {
|
||||
sb.append(line);
|
||||
sb.append("\n");
|
||||
line = br.readLine();
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
br.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.memoryleaks.internedstrings;
|
||||
|
||||
public class StringObject {
|
||||
private static final String FILEPATH = "C:\\bigstring.txt";
|
||||
|
||||
public void readString() {
|
||||
String s1 = ReadStringFromFileUtil.read(FILEPATH);
|
||||
String s2 = ReadStringFromFileUtil.read(FILEPATH);
|
||||
|
||||
if (s1 == s2) {
|
||||
System.out.println("Both the strings objects are same");
|
||||
}
|
||||
else {
|
||||
System.out.println("Both the strings objects are different");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.memoryleaks.staticfields;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NonStaticFieldsDemo {
|
||||
public List<Double> list = new ArrayList<>();
|
||||
|
||||
public void populateList() {
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
list.add(Math.random());
|
||||
}
|
||||
System.out.println("Debug Point 2");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Debug Point 1");
|
||||
new NonStaticFieldsDemo().populateList();
|
||||
System.out.println("Debug Point 3");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.memoryleaks.staticfields;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class StaticFieldsDemo {
|
||||
public static List<Double> list = new ArrayList<>();
|
||||
|
||||
public void populateList() {
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
list.add(Math.random());
|
||||
}
|
||||
System.out.println("Debug Point 2");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Debug Point 1");
|
||||
new StaticFieldsDemo().populateList();
|
||||
System.out.println("Debug Point 3");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.outofmemoryerror;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class OutOfMemoryGCLimitExceed {
|
||||
public static void addRandomDataToMap() {
|
||||
Map<Integer, String> dataMap = new HashMap<>();
|
||||
Random r = new Random();
|
||||
while (true) {
|
||||
dataMap.put(r.nextInt(), String.valueOf(r.nextInt()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
OutOfMemoryGCLimitExceed.addRandomDataToMap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.memoryleaks.equalshashcode;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PersonMemoryLeakUnitTest {
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenMap_whenEqualsAndHashCodeNotOverridden_thenMemoryLeak() {
|
||||
Map<Person, Integer> map = new HashMap<Person, Integer>();
|
||||
for(int i=0; i<10000000; i++) {
|
||||
map.put(new Person("jon"), 1);
|
||||
}
|
||||
assertTrue(map.size() > 1);
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenMap_whenEqualsAndHashCodeOverridden_thenNoMemoryLeak() {
|
||||
Map<PersonOptimized, Integer> map = new HashMap<PersonOptimized, Integer>();
|
||||
for(int i=0; i<10000; i++) {
|
||||
map.put(new PersonOptimized("jon"), 1);
|
||||
}
|
||||
assertTrue(map.size() == 1);
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.memoryleaks.finalize;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FinalizeMemoryLeakUnitTest {
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenObjectWithFinalizer_whenCreatingAndDestroyingThisObject_thenMemoryLeak() {
|
||||
BulkyObject[] stock = new BulkyObject[100000];
|
||||
|
||||
for(int i=0; i<100000; i++) {
|
||||
stock[i] = new BulkyObject();
|
||||
}
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenObjectWithoutFinalizer_whenCreatingAndDestroyingThisObject_thenNoMemoryLeak() {
|
||||
BulkyObjectOptimized[] stock = new BulkyObjectOptimized[100000];
|
||||
|
||||
for(int i=0; i<100000; i++) {
|
||||
stock[i] = new BulkyObjectOptimized();
|
||||
}
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StaticInnerClassMemoryLeakUnitTest {
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenUsingInnerClass_whenInitializingInnerClass_thenInnerClassHoldsReferenceOfOuterObject() {
|
||||
InnerClassWrapper.SimpleInnerClass simpleInnerClassObj = new InnerClassWrapper().new SimpleInnerClass();
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenUsingStaticNestedClass_whenInitializingInnerClass_thenStaticNestedClassDoesntReferenceOuterObject() {
|
||||
StaticNestedClassWrapper.StaticNestedClass staticNestedClassObj = new StaticNestedClassWrapper.StaticNestedClass();
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.memoryleaks.internedstrings;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringInternMemoryLeakUnitTest {
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenJava6OrBelow_whenInterningLargeStrings_thenPermgenIncreases() {
|
||||
new InternedString().readString();
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenJava6OrBelow_whenNotInterningLargeStrings_thenPermgenDoesntIncrease() {
|
||||
new StringObject().readString();
|
||||
System.out.print("Debug Point - VisuaLVM");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.memoryleaks.staticfields;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NonStaticFieldsMemoryLeakUnitTest {
|
||||
public List<Double> list = new ArrayList<>();
|
||||
|
||||
public void populateList() {
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
list.add(Math.random());
|
||||
}
|
||||
System.out.println("Debug Point 2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenNonStaticLargeList_whenPopulatingList_thenListGarbageCollected() {
|
||||
System.out.println("Debug Point 1");
|
||||
new NonStaticFieldsMemoryLeakUnitTest().populateList();
|
||||
System.out.println("Debug Point 3");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.memoryleaks.staticfields;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StaticFieldsMemoryLeakUnitTest {
|
||||
public static List<Double> list = new ArrayList<>();
|
||||
|
||||
public void populateList() {
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
list.add(Math.random());
|
||||
}
|
||||
System.out.println("Debug Point 2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // Test deliberately ignored as memory leak tests consume lots of resources
|
||||
public void givenStaticLargeList_whenPopulatingList_thenListIsNotGarbageCollected() {
|
||||
System.out.println("Debug Point 1");
|
||||
new StaticFieldsDemo().populateList();
|
||||
System.out.println("Debug Point 3");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user