├── LICENSE
├── README.md
├── ch2
├── .gitkeep
├── CallByValue.java
├── EscapingReferences.java
└── StackAndHeap.java
└── ch7
├── .gitkeep
└── OutOfMemoryExample.java
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Packt
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java Memory Management
2 |
3 |
4 |
5 | This is the code repository for [Java Memory Management](https://www.amazon.com/dp/1801812853), published by Packt.
6 |
7 | **A comprehensive guide to garbage collection and JVM tuning**
8 |
9 | ## What is this book about?
10 | Understanding how Java organizes memory is important for every Java professional, but this particular topic is a common knowledge gap for many software professionals. Having in-depth knowledge of memory functioning and management is incredibly useful in writing and analyzing code, as well as debugging memory problems. In fact, it can be just the knowledge you need to level up your skills and career.
11 |
12 | This book covers the following exciting features:
13 | * Understand the schematics of debugging and how to design the application to perform well
14 | * Discover how garbage collectors work
15 | * Distinguish between various garbage collector implementations
16 | * Identify the metrics required for analyzing application performance
17 | * Configure and monitor JVM memory management
18 | * Identify and solve memory leaks
19 |
20 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1801812853) today!
21 |
22 |
24 |
25 | ## Instructions and Navigations
26 | All of the code is organized into folders. For example, Chapter04.
27 |
28 | The code will look like the following:
29 | ```
30 | Object o = new Object();
31 | System.out.println(o);
32 | o = null;
33 | ```
34 |
35 | **Following is what you need for this book:**
36 | This book is for all levels of Java professionals, regardless of whether you’re a junior or senior developer, a DevOps engineer, a tester, or the system admin of a Java application. If you currently don't have in-depth knowledge of Java memory, garbage collection, and/or JVM tuning, then this book will help you to take your Java skills to the next level.
37 |
38 | With the following software and hardware list you can run all code files present in the book (Chapter 1-7).
39 | ### Software and Hardware List
40 | | Chapter | Software required | OS required |
41 | | -------- | ------------------------------------ | ----------------------------------- |
42 | | 1-7 | Java 8+ | Windows, Mac OS X, and Linux (Any) |
43 |
44 |
45 | We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://packt.link/OeQqF).
46 |
47 | ## Errata:
48 |
49 | * Page 102: -XX:+G1GC _should be_ -XX:+UseG1GC
50 |
51 | ### Related products
52 | * Domain-Driven Design with Java - A Practitioner’s Guide [[Packt]](https://www.packtpub.com/product/domain-driven-design-with-java-a-practitioners-guide/9781800560734?utm_source=github&utm_medium=repository&utm_campaign=9781800560734) [[Amazon]](https://www.amazon.com/dp/1800560737)
53 |
54 | * Learn Java 17 Programming - Second Edition [[Packt]](https://www.packtpub.com/product/learn-java-17-programming-second-edition/9781803241432?utm_source=github&utm_medium=repository&utm_campaign=9781803241432) [[Amazon]](https://www.amazon.com/dp/1803241438)
55 |
56 | ## Get to Know the Authors
57 | **Maaike van Putten**
58 | is an experienced software developer and trainer with a passion for software development and helping others to get to the next level in their career. Some of her favorite languages are Java, JavaScript, and Python. She participates as a developer in software development projects and teaches a lot of training courses, ranging from IT for beginners to advanced topics for senior software developers. She also loves to create online content to help a larger audience, and she does so for diverse platforms such as Pluralsight, LinkedIn Learning, and Udemy.
59 |
60 | **Seán Kennedy**
61 | is a university lecturer with over 20 years of experience in teaching. He has a PhD in IT and is Oracle-certified in Java at the Professional level (OCP). In his daily work, he teaches Java on a bespoke master’s program for a highly regarded software company. He has a YouTube channel called Let’s Get Certified that teaches Java at all levels and prepares candidates for Java certification. He also has similar courses on Udemy. Outside of work, he enjoys tennis, walking, nature, reading, and TV.
62 |
63 | ### Download a free PDF
64 |
65 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
66 |
https://packt.link/free-ebook/9781801812856
67 | -------------------------------------------------------------------------------- /ch2/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ch2/CallByValue.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | class Person{ 4 | private String name; 5 | private int age; 6 | 7 | Person(String name, int age) { 8 | this.age = age; 9 | this.name = name; 10 | } 11 | public String getName() { 12 | return name; 13 | } 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | public int getAge() { 18 | return age; 19 | } 20 | public void setAge(int age) { 21 | this.age = age; 22 | } 23 | } 24 | 25 | public class CallByValue { 26 | public static void main(String[] args) { 27 | int age=20; 28 | Person john = new Person("John", age); 29 | change(john, age); 30 | System.out.println(john.getName() 31 | + " " + age); // Michael 20 32 | } 33 | public static void change(Person adult, int age){ 34 | age = 90; 35 | adult.setName("Michael"); 36 | } 37 | } 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /ch2/EscapingReferences.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | class Person { 4 | private StringBuilder name; 5 | 6 | Person(StringBuilder name) { 7 | this.name = new StringBuilder(name.toString()); 8 | } 9 | public StringBuilder getName() { 10 | return new StringBuilder(name.toString()); 11 | } 12 | } 13 | 14 | public class EscapingReferences { 15 | public static void main(String[] args) { 16 | StringBuilder sb = new StringBuilder("Dan"); 17 | Person p = new Person(sb); 18 | sb.append("Dan"); 19 | System.out.println(p.getName()); // Dan 20 | 21 | StringBuilder sb2 = p.getName(); 22 | sb2.append("Dan"); 23 | System.out.println(p.getName()); // Dan 24 | } 25 | } 26 | 27 | 28 | // the issue: Fig 2.10 29 | //class Person { 30 | // private StringBuilder name; 31 | // 32 | // Person(StringBuilder name) { 33 | // this.name = name; 34 | // } 35 | // public StringBuilder getName() { 36 | // return name; 37 | // } 38 | //} 39 | // 40 | //public class EscapingReferences { 41 | // public static void main(String[] args) { 42 | // StringBuilder sb = new StringBuilder("Dan"); 43 | // Person p = new Person(sb); 44 | // sb.append("Dan"); 45 | // System.out.println(p.getName()); // DanDan 46 | // 47 | // StringBuilder sb2 = p.getName(); 48 | // sb2.append("Dan"); 49 | // System.out.println(p.getName()); // DanDanDan 50 | // } 51 | //} 52 | 53 | // the solution: Fig. 2.13 54 | //class Person { 55 | // private StringBuilder name; 56 | // 57 | // Person(StringBuilder name){ 58 | // this.name = new StringBuilder(name.toString()); 59 | // } 60 | // public StringBuilder getName() { 61 | // return new StringBuilder(name.toString()); 62 | // } 63 | //} 64 | // 65 | //public class EscapingReferences { 66 | // public static void main(String[] args) { 67 | // StringBuilder sb = new StringBuilder("Dan"); 68 | // Person p = new Person(sb); 69 | // sb.append("Dan"); 70 | // System.out.println(p.getName()); // Dan 71 | // 72 | // StringBuilder sb2 = p.getName(); 73 | // sb2.append("Dan"); 74 | // System.out.println(p.getName()); // Dan 75 | // } 76 | //} 77 | 78 | 79 | -------------------------------------------------------------------------------- /ch2/StackAndHeap.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | interface Walkable{} 3 | 4 | abstract class Human{} 5 | 6 | class Person extends Human implements Walkable{ // Fig. 2.3 7 | private String name; 8 | private int age; 9 | 10 | Person(String aName, int aAge){ 11 | name = aName; 12 | age = aAge; 13 | } 14 | @Override 15 | public String toString(){ 16 | String decoratedName = "My name is "+name + 17 | " and I am "+ age + " years old."; 18 | return decoratedName; 19 | } 20 | } 21 | 22 | public class StackAndHeap { 23 | public static void main(String[] args) { 24 | int x=0; 25 | Person joeBloggs = new Person("Joe Bloggs", 23); 26 | System.out.println(x); 27 | System.out.println(joeBloggs.toString()); 28 | } 29 | } 30 | 31 | /* 32 | Fig 2.1 33 | public class StackAndHeap { 34 | public static void main(String[] args) { 35 | Human h; 36 | h = new Human(); 37 | 38 | Walkable w; 39 | w = new Walkable(); 40 | } 41 | } 42 | 43 | */ 44 | /* 45 | Fig 2.2 46 | public class StackAndHeap { 47 | public static void main(String[] args) { 48 | Human h; 49 | h = new Person(); 50 | System.out.println(h.toString()); 51 | 52 | Walkable w; 53 | w = new Person(); 54 | System.out.println(w.toString()); 55 | } 56 | } 57 | 58 | */ 59 | -------------------------------------------------------------------------------- /ch7/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ch7/OutOfMemoryExample.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class Person{ 7 | private String name; 8 | Person(String aName){ 9 | name = aName; 10 | } 11 | } 12 | public class OutOfMemoryExample { 13 | public static void main(String[] args) { 14 | List