0% found this document useful (0 votes)
3 views

Mastering Java Memory Management and Garbage Collection

The document presents an overview of memory management and garbage collection in Java, detailing the Java Memory Model, including stack and heap memory. It explains key concepts of garbage collection, including generational garbage collection and various algorithms used to manage memory efficiently. Additionally, it provides best practices for optimizing memory usage and performance in Java applications.

Uploaded by

10pchakraborty
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Mastering Java Memory Management and Garbage Collection

The document presents an overview of memory management and garbage collection in Java, detailing the Java Memory Model, including stack and heap memory. It explains key concepts of garbage collection, including generational garbage collection and various algorithms used to manage memory efficiently. Additionally, it provides best practices for optimizing memory usage and performance in Java applications.

Uploaded by

10pchakraborty
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Mastering Java PRESENTED BY-

PRITAM CHAKRABORTY
Memory STREAM- CSE
SECTION- A
Management and ROLL NUMBER-
12100122045
Garbage Collection

--Inside Java's Memory Engine--


JAVA
Imagine your computer's memory as a well-
organized library. Java is the librarian ensuring

TOPICS
that books are efficiently stored and removed
when no longer needed.

• Introduction • Generational Garbage Collection


• The Java Memory Model
• Best Practices for Memory Managem
• Stack Memory ent
• Heap Memory
• Conclusion
• Understanding Garbage
Collection • References
• Key Concepts in GC
JAVA

The Java
Memory Model
• Stack Memory
• Heap Memory
Stack
JAVA

• The stack memory is a physical space (in RAM)

Memory
allocated to each thread at run time

• It is created when a thread creates.

• Memory management in the stack follows LIFO (Last-


In-First-Out) order because it is accessible globally.
• Memory allocated to stack lives until the function
returns. If there is no space for creating the new
objects, it throws the java.lang.StackOverFlowError

• The scope of the elements is limited to their threads.


The JVM creates a separate stack for each thread.
Heap
JAVA

• It is created when the JVM starts up and used by the

Memory
application as long as the application runs.

• It stores objects and JRE classes.

• It does not follow any order like the stack.

• It dynamically handles the memory blocks.

• or managing the memory automatically, Java


provides the garbage collector that deletes the
objects which are no longer being used.

• If the heap space is full, it throws the


java.lang.OutOfMemoryError.
Understanding
Garbage Collection
• Automated Memory Management: GC automatically reclaims
memory from unused objects, preventing leaks and conserving
memory.
• GC Algorithms: Techniques like Mark-and-Sweep and Generational
Collection manage memory differently, affecting efficiency.
• Performance Impact: GC can introduce overhead, impacting
application performance and responsiveness.
• Tuning: Adjusting GC settings can optimize memory use and
application performance.

Tip: GC is like a janitor in our memory warehouse, cleaning up unneeded items to make space for new ones.
Garbage Collection
(GC) Key Concepts

Tracing Collectors & Heap


Management Finalization &
Automatic Memory
Tuning
Management
• Mark-and-Sweep: Marks reachable
objects; collects unmarked ones
• Allows cleanup actions
• Reclaims unused memory • Mark-and-Compact: Compacts
through finalizers or
and prevents leaks memory to reduce fragmentation
• Utilizes techniques like • Generational Collection: Segments destructors
reference counting and • Adjusts GC parameters for
objects by age; focuses on young
tracing performance and memory
objects
balance
• Manages dynamic memory
allocation and optimizes space
Generational
Garbage Collection
1980s 2002 2011 2018-Present

Introduction of Java 1.4 and .NET Java 7 introduced the G1 Java 11 introduced ZGC
generational garbage Framework adopted Garbage Collector, and Shenandoah GC,
generational GC, with enhancing generational focusing on reducing
collection concepts,
Java's HotSpot JVM GC with low-latency pause times and
focusing on separating
implementing young and optimizations. improving performance
objects by age to
old generations, while incorporating
improve memory and .NET using three generational GC
management. generations (0, 1, and principles
2).
Young • Young Generation GC focuses on
collecting short-lived objects, initially
allocated in the Eden space and moved
Generation GC to Survivor spaces.

• Minor GC occurs when the Eden space is full, promoting


surviving objects to the Survivor spaces or the Old
Generation if they persist.

• Focuses on short-lived objects.


• Frequent and fast.

• Frequent Minor GCs can be indicative of memory allocation


issues or high object churn.
Tip: Monitor the frequency of Minor GCs to ensure your application isn't generating excessive short-lived
objects that could impact performance
Old Generation • Old Generation GC deals with long-lived
objects that have been promoted from

GC the Young Generation after surviving


multiple Minor GCs

• Major GC or Full GC is triggered when


the Old Generation space is full,
involving a more comprehensive
cleanup and compaction process.

• Less frequent but more time-


consuming.
• Cleans up long-lived objects.

• Extended Full GC pauses can


significantly impact application
performance, especially if the Old
Generation is heavily fragmented.
Tip: Optimize object retention and consider tuning the JVM heap size to reduce the frequency and impact
of Full GCs.
Smart Memory Allocation Methods
Minimize Object Creation Use Primitive Types: Manage Collections Wisely: Release Resources Promptly:

• Close or release resources


• Reuse objects where possible • Prefer primitives over boxed • Use appropriate collection
(e.g., files, connections) as
to reduce allocation types to avoid unnecessary types and manage their sizes
soon as they are no longer
overhead. memory usage. effectively.
needed.

Avoid Memory Leaks Use Weak References: Optimize Garbage Collection: Profile Memory Usage:

• Ensure that objects are


• Utilize weak references for • Tune garbage collection • Regularly profile and analyze
dereferenced when no longer
objects that can be collected settings based on application memory usage to identify
needed to allow garbage
when memory is low. needs and workload. and address inefficiencies.
collection.

Minimize Object Retention: Prefer Stack Allocation: Limit Thread Count: Optimize Data Structures:

• Avoid holding references to • Use stack allocation for • Choose memory-efficient


• Avoid creating excessive
objects longer than temporary objects instead of data structures and
threads that can lead to high
necessary to reduce memory heap allocation when algorithms suited to the
memory consumption.
footprint. feasible. specific use case.
Tip: Focus on the introduction and key sections for

Resources
essential concepts.

How: Apply the concepts with practical examples or


experiments.

java the complete edition - Schildt ibm.com

"Efficient Memory Management in Java"


Oracle Java Documentation
(Article by InfoQ)

"Garbage Collection: Algorithms for


javapoint.com Automatic Dynamic Memory Management"
by Richard Jones and Antony Hosking

You might also like