JAVA MEMORY MANAGEMENT
Overview
•Java performs automatic memory
management,
•Shielding the developer from the
complexity of explicit memory management
•This session
•describes
the garbage collectors available to perform the memory management, and
•gives some advice regarding
choosing and configuring a collector and setting sizes for the memory areas on
which the collector operates
Explicit
vs. Automatic Memory Management
•Memory management is the process of
recognizing when allocated objects are no longer needed, deallocating (freeing)
the memory used by such objects, and making it available for subsequent
allocations
•Explicit
memory management:
•Developer‘s
responsibility
•Problems:
•Dangling
pointers, space leak
•Automatic memory management enables
increased abstraction of interfaces and more reliable code.
•Automatic management by a program
called a garbage collector
•Avoids the dangling reference
problem, because an object that is still referenced somewhere will never be
garbage collected and so will not be considered free.
•Solves the space leak problem since
it automatically frees all memory no longer referenced
Garbage
Collection Concepts
•Responsibilities:
•Allocating
memory
•Ensuring
that any referenced objects remain in memory, and
•Recovering memory used by objects
that are no longer reachable from references in executing code
•Objects
that are referenced are said to be live
•Objects
that are no longer referenced are considered dead and are termed garbage
•Garbage collection
•The process of finding and freeing
(also known as reclaiming) the space used by dead objects
•Garbage collection is also a
complex task taking time and resources of its own
•Space is
commonly allocated from a large pool of memory referred to as the heap
•The
precise algorithm used to organize memory and allocate and deallocate space is
hidden from the programmer
•The timing of garbage collection is
up to the garbage collector
•Typically, the entire heap or a
subpart of it is collected either when it fills up or when it reaches a
threshold percentage of occupancy
•The task
of fulfilling an allocation request, which involves finding a block of unused
memory of a certain size in the heap, is a difficult one
•The main problem for most dynamic
memory allocation algorithms is to avoid fragmentation, while keeping both
allocation and deallocation efficient
Generational
Collection
•memory
is divided into generations, that is, separate pools holding objects of
different ages
•Different algorithms can be used to
perform garbage collection in the different generations, each algorithm
optimized based on commonly observed characteristics for that particular
generation
Allocating
an object memory
•Bump-the-pointer technique:
•Within a
generation, there are large contiguous blocks of memory available from which to
allocate objects
•The end
of the previously allocated object is always kept track of
•When a new allocation request needs
to be satisfied, all that needs to be done is to check whether the object will
fit in the remaining part of the generation and, if so, to update the pointer
and initialize the object
•For multithreaded applications,
allocation operations need to be multithread-safe
•Global locks impose bottleneck
•Thread-Local Allocation Buffers
(TLABs):
•Each
thread is given its own buffer (i.e., a small portion of the generation) from
which to allocate
•Since
only one thread can be allocating into each TLAB, allocation can take place
quickly by utilizing the bump-the-pointer technique, without requiring any
locking
•Only infrequently, when a thread
fills up its TLAB and needs to get a new one, must synchronization be utilized
•Thread-Local Allocation Buffers
(contd..):
•Several techniques to minimize
space wastage due to the use of TLABs are employed
•For
example, TLABs are sized by the allocator to waste less than 1% of Eden , on average
•The combination of the use of TLABs
and linear allocations using the bump-the-pointer technique enables each
allocation to be efficient, only requiring around 10 native instructions
J2SE 5.0
HotSpot JVM collectors
•Four garbage collectors
•Serial
Collector
•Parallel
Collector
•Parallel
Compacting Collector
•Concurrent Mark-Sweep (CMS)
Collector
•All are generational
No comments:
Post a Comment