JAVA MEMORY
the Java GC and memory model
GARBAGE COLLECTOR
• With Java GC and the
memory model we can
forget about the
memory allocation
problems, deallocation
and memory leaks…
YES?
maybe no…
THE PERFORMANCE
• Java garbage collection seems to work too well,
creating and removing too many objects.
• Most memory-management issues are solved, but often
at the cost of creating serious performance problems
MEMORY AREAS (J7 HOTSPOT)
• The heap - dynamically
memory allocation
• The stack - where
threads life
• PermGen - owns Java
Hotspot metadata
class Main {
private static int a;
private int b;
private AnObject o = new Object();
public int method(int i, OtherMore om) {
int z;
OtherObject p;
…
}
}
heap
stack
• LIFO
• not GC
• each thread has its own stack
portion
main a b o
o (ref)
• primitive & instance
• objects & classes
• GCshared per instance
i
om (ref)
om
z
p
p (ref)
code
class Main {
private static int a;
private int b;
private AnObject o = new Object();
public int method(int i, OtherMore om) {
int z;
OtherObject p;
…
}
}
heap
stack
main a b o
o (ref)
shared per instance
om p
code
When we exit the method the
stack pointer just “lost” all
references (nullify is useless)
methodA {
new B
}
THE METHOD IN A THREAD CREATE NEW OBJECTS
ALSO IN A SINGLETON OBJECT!
BankiaLinkPasswordHelper
methodA
singleton
B
B
B
B
methodA {
new B
}
methodA {
new B
}
methodA {
new B
}
methodA {
new B
}
B
heap
main a b o
shared per instance
om p
GC runs only on heap!
Young are short-live objects and GC runs here a lot
but in old, are objects that survive a lot and GC runs little on this
objects
Usually GC has two “main zones”: young & old
GC SPACES
1. The majority of newly created objects are located
in the Eden space.
2. After one GC in the Eden space, the surviving
objects are moved to one of the Survivor
spaces. 
3. After a GC in the Eden space, the objects are
piled up into the Survivor space, where other
surviving objects already exist.
4. Once a Survivor space is full, surviving objects are
moved to the other Survivor space.Then, the
Survivor space that is full will be changed to a
state where there is no data at all.
5. The objects that survived these steps
that have been repeated a number of
times are moved to the old
generation.
GC OLD GENERATION
• The old generation basically
performs a GC when the data is
full
• JDK7: Garbage Collector of Server
VM Changed to Parallel Garbage
Collector; on server-class
machines running the serverVM,
the garbage collector (GC) has
changed from the previous serial
collector (-XX:+UseSerialGC) to
a parallel collector (-XX:
+UseParallelGC).
EXAMPLE
heap
main a b o
shared per instance
om p
young
po o o om
stack (threads)
old
heap
main a b o
shared per instance
p
young
pom
stack (threads)
old
Not referenced objects in “OLD” may
stay for more time
GC will free not-in-use areas
om o o o
OBJECT ROOTS
A GC root is a reference
which only has outgoing and
no incoming references
Every object we allocate within the scope of our execution
will be freed up automatically after leaving the method scope.
So memory leaks are caused by references which exist
beyond our current execution scope like Servlet sessions or
caches and any objects stored in static references.
KEY POINTS TO LEARN
• GC does not get rid of unreachable objects
• GC do mark and maintain alive objects
• GC do not work on “old” unless max
memory is reached
• GC stop all when is search&marking objects for
survive
• Having a huge amount of objects is what
impact on performance
• Carefull with objects not in the execution scope
(global or statics)
GC DEFRAG
• GC does the memory
defrag in their tasks
G1
• "Garbage First” (G1)
collector, HotSpot's
latest GC (introduced
in JDK7 update 4)
• In G1 GC, HotSpot
introduces the
concept of “regions”
THREAD SAFE IS NOT
CONCURRENCY
• Thread-safe is a mechanism for concurrency
You can have thread safe classes and a terrible
concurrency (extreme = all synchronized)
You can’t have concurrency without thread safe
@earroyoron
PARLA

Java Garbage Collector and The Memory Model

  • 1.
    JAVA MEMORY the JavaGC and memory model
  • 2.
    GARBAGE COLLECTOR • WithJava GC and the memory model we can forget about the memory allocation problems, deallocation and memory leaks…
  • 3.
  • 4.
    THE PERFORMANCE • Javagarbage collection seems to work too well, creating and removing too many objects. • Most memory-management issues are solved, but often at the cost of creating serious performance problems
  • 5.
    MEMORY AREAS (J7HOTSPOT) • The heap - dynamically memory allocation • The stack - where threads life • PermGen - owns Java Hotspot metadata
  • 6.
    class Main { privatestatic int a; private int b; private AnObject o = new Object(); public int method(int i, OtherMore om) { int z; OtherObject p; … } } heap stack • LIFO • not GC • each thread has its own stack portion main a b o o (ref) • primitive & instance • objects & classes • GCshared per instance i om (ref) om z p p (ref) code
  • 7.
    class Main { privatestatic int a; private int b; private AnObject o = new Object(); public int method(int i, OtherMore om) { int z; OtherObject p; … } } heap stack main a b o o (ref) shared per instance om p code When we exit the method the stack pointer just “lost” all references (nullify is useless)
  • 8.
    methodA { new B } THEMETHOD IN A THREAD CREATE NEW OBJECTS ALSO IN A SINGLETON OBJECT! BankiaLinkPasswordHelper methodA singleton B B B B methodA { new B } methodA { new B } methodA { new B } methodA { new B } B
  • 9.
    heap main a bo shared per instance om p GC runs only on heap! Young are short-live objects and GC runs here a lot but in old, are objects that survive a lot and GC runs little on this objects Usually GC has two “main zones”: young & old
  • 10.
    GC SPACES 1. Themajority of newly created objects are located in the Eden space. 2. After one GC in the Eden space, the surviving objects are moved to one of the Survivor spaces.  3. After a GC in the Eden space, the objects are piled up into the Survivor space, where other surviving objects already exist. 4. Once a Survivor space is full, surviving objects are moved to the other Survivor space.Then, the Survivor space that is full will be changed to a state where there is no data at all. 5. The objects that survived these steps that have been repeated a number of times are moved to the old generation.
  • 11.
    GC OLD GENERATION •The old generation basically performs a GC when the data is full • JDK7: Garbage Collector of Server VM Changed to Parallel Garbage Collector; on server-class machines running the serverVM, the garbage collector (GC) has changed from the previous serial collector (-XX:+UseSerialGC) to a parallel collector (-XX: +UseParallelGC).
  • 12.
  • 13.
    heap main a bo shared per instance om p young po o o om stack (threads) old
  • 14.
    heap main a bo shared per instance p young pom stack (threads) old Not referenced objects in “OLD” may stay for more time GC will free not-in-use areas om o o o
  • 15.
    OBJECT ROOTS A GCroot is a reference which only has outgoing and no incoming references Every object we allocate within the scope of our execution will be freed up automatically after leaving the method scope. So memory leaks are caused by references which exist beyond our current execution scope like Servlet sessions or caches and any objects stored in static references.
  • 16.
    KEY POINTS TOLEARN • GC does not get rid of unreachable objects • GC do mark and maintain alive objects • GC do not work on “old” unless max memory is reached • GC stop all when is search&marking objects for survive • Having a huge amount of objects is what impact on performance • Carefull with objects not in the execution scope (global or statics)
  • 17.
    GC DEFRAG • GCdoes the memory defrag in their tasks
  • 18.
    G1 • "Garbage First”(G1) collector, HotSpot's latest GC (introduced in JDK7 update 4) • In G1 GC, HotSpot introduces the concept of “regions”
  • 19.
    THREAD SAFE ISNOT CONCURRENCY • Thread-safe is a mechanism for concurrency You can have thread safe classes and a terrible concurrency (extreme = all synchronized) You can’t have concurrency without thread safe
  • 20.