Professional Documents
Culture Documents
Garbage Collector: Conociendo El Manejo de Memoria en El JVM
Garbage Collector: Conociendo El Manejo de Memoria en El JVM
@SuperSerch
¿Qué ocurre en la memoria?
¿Qué ocurre en la memoria?
¿Qué ocurre en la memoria?
¿Qué ocurre en la memoria?
¿Qué ocurre en la memoria?
¿Qué ocurre en la memoria?
¿Qué ocurre en la memoria?
¿Qué ocurre en la memoria?
Retos al administrar la memoria
Fragmentación de la memoria
JVMS: 2.5.3 Heap
The heap is created on virtual machine start-up. Heap storage for objects
is reclaimed by an automatic storage management system (known as a
garbage collector); objects are never explicitly deallocated. The Java
Virtual Machine assumes no particular type of automatic storage
management system, and the storage management technique may be
chosen according to the implementor's system requirements. The heap may
be of a fixed size or may be expanded as required by the computation and
may be contracted if a larger heap becomes unnecessary. The memory for
the heap does not need to be contiguous.
JVMS: 2.5.3 Heap
The heap is created on virtual machine start-up. Heap storage for objects
is reclaimed by an automatic storage management system (known as a
garbage collector); objects are never explicitly deallocated. The Java
Virtual Machine assumes no particular type of automatic storage
management system, and the storage management technique may be
chosen according to the implementor's system requirements. The heap may
be of a fixed size or may be expanded as required by the computation and
may be contracted if a larger heap becomes unnecessary. The memory for
the heap does not need to be contiguous.
If a computation requires more heap than can be made available by the
automatic storage management system, the Java Virtual Machine
throws an OutOfMemoryError.
Hipótesis Generacional
Hipótesis generacional debil
( jdk8 - nativo)
Old Gen
PermGen
Young Gen
Young Gen
Young Gen
Eden
Survivor Spaces
From⇄To
Allocation
Allocation
Allocation
From To
Allocation
From To
Allocation
To From
Allocation
To From
Allocation
From To
Promotion
From To
From To
Old Generation
Colectores en Oracle JVM
Alto throughput
GC Root de la jvm
GC Roots de la JVM
Fuerte
Suave ( SoftReference )
Debil ( WeakReference )
Fantasma ( PhantomReference )
Ejemplo WeakReference
Stack Heap
WagTask.run Kennel.dogCache
WeakReference
Dog
Tail
Ejemplo WeakReference
Stack Heap
WagTask.run Kennel.dogCache
WeakReference
El Thread se muere
Dog
Tail
Ejemplo WeakReference
Heap
Kennel.dogCache
Durante un GC WeakReference
Dog
Tail
Ejemplo WeakReference
Heap
Kennel.dogCache
null
Problemas comunes
(y como evitarlos)
Memory leak
public void push(Object e) {
ensureCapacity();
elements[size++] = e;
}
vector.clear();
...
Nonlocal Instance Field
public class Storer {
private Map<Integer, String> hm =
new HashMap<>();
class HashMetaData {
private Map<WeakReference<SSLSocket>, InetAddress>
m = Collections.synchronizedMap( new HashMap<>());
ReferenceQueue queue = new ReferenceQueue();
...
Referencias fuertes
public void storeTempConn(SSLSocket sock,
InetAddress ip) {
WeakReference<SSLSocket> wr;
while((wr = (WeakReference) queue.poll) != null){
//...
m.remove(wr);
}
wr = new WeakReference<>(sock, queue);
m.put(wr, ip);
}
...
Referencias fuertes
public void storeTempConn(SSLSocket sock,
InetAddress ip) {
SoftReference<SSLSocket> sr;
while((sr = (SoftReference) queue.poll) != null){
//...
m.remove(sr);
}
sr = new SoftReference<>(sock, queue);
m.put(sr, ip);
}
...
¿ Preguntas ?
Bibliografía
Efective Java Second Edition - Joshua Bloch