Top Banner
Weak Pharo Story Pavel Krivanek & Guille Polito A
42

A Weak Pharo Story

Apr 14, 2017

Download

Software

ESUG
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Weak Pharo Story

Weak Pharo Story

Pavel Krivanek & Guille Polito

A

Page 2: A Weak Pharo Story

Smalltalk is a

GC'edlanguage

Page 3: A Weak Pharo Story

Two kind of leaks

●  Leak application objectse.g., your domain objects, collections...

●  Leak external objectse.g., sockets, files, memory allocated in C heap

Page 4: A Weak Pharo Story

Root objects hold yours!

These two guys in the red area are never going to be collected

Page 5: A Weak Pharo Story

But... we have Weak References

Page 6: A Weak Pharo Story

Weak References In One Slide

Not Collected

Page 7: A Weak Pharo Story

Weak References In One Slide

Not Collected

Page 8: A Weak Pharo Story

What about external objects?

File id

Who closes the file if it gets collected?

Page 9: A Weak Pharo Story

Finalization

There is a registry of“Objects to be notified when about to be 

collected”

WeakRegistry default add: theInterestedGuy

Page 10: A Weak Pharo Story

Object Finalization

File id

The registry notifies

finalize

close

Page 11: A Weak Pharo Story

But NONE of it is

Page 12: A Weak Pharo Story

[ WARNING]

The following images can affect sensitive people

Page 13: A Weak Pharo Story

No matter how weakyour references are

Memory Leakswill find you

Page 14: A Weak Pharo Story

The  Weak Pharo Story (finally)

O nce upon a time, there was 

Announcements, an event delivery library, 

that the princess named Engineer used to 

notify myObject from anEvent

announcer when: anEvent send: #message to: myObject

Page 15: A Weak Pharo Story

The  Weak Pharo Story (II)

But Engineer did love myObject so much 

that it did not want to retain it for ever. It did 

not want announcer to hold myObject

strongly. She wanted a weak announcer.

announcer weak when: anEvent send: #message to: myObject

Page 16: A Weak Pharo Story

The  Weak Pharo Story (III)

However, Engineer did not know this may 

curse myObject to be alive for the eternity. 

And never be collected and see his friends 

die. And create OutOfMemory errors on the 

land of objects to torment the rest of the 

objects.

The end

Page 17: A Weak Pharo Story

Case 1: The Strong Announcer

Page 18: A Weak Pharo Story

Case 1: The Strong Announcer

Page 19: A Weak Pharo Story

Case 2: The Weak Announcer

Page 20: A Weak Pharo Story

Case 2: The Weak Announcer

Page 21: A Weak Pharo Story

Case 3: The  Hybrid Announcer

Page 22: A Weak Pharo Story

Case 3: The  Hybrid Announcer

Page 23: A Weak Pharo Story

Case 3: The  Hybrid Announcer

Page 24: A Weak Pharo Story

Autopsy

● Weak references do not simply avoid leaks!

● Finalization itself can create leaks!

Page 25: A Weak Pharo Story

So... solutions?

1) How do we detect leaks?

2) How do we prevent some?

Page 26: A Weak Pharo Story

#1 ­ Detecting Leaks

Page 27: A Weak Pharo Story

Memory leaks investigation

Why ???

Page 28: A Weak Pharo Story

anObject pointersTo

● Very inefficient

SystemNavigation default allObjectsDo: [:e |(e pointsTo: self) ifTrue: [

pointers add: e ]].

Page 29: A Weak Pharo Story

Easy to get lost

anObject pointersTo first pointersTo first pointersTo second pointersTo last...

Page 30: A Weak Pharo Story

Open pointers to...

● Easy to use for simple cases● Uses #pointersTo● References from tools more mess▶

Page 31: A Weak Pharo Story

Hell of announcements and weak references

Page 32: A Weak Pharo Story

RefsHunter

● Temporary snapshot of the object memory

Page 33: A Weak Pharo Story

RefsHunter

● Shows the shortest path from one object to another

● Fast queries

   rh := RefsHunter snapshot.   rh wayFrom: (Array>>#asArray)      to: Smalltalk specialObjectsArray.

Page 34: A Weak Pharo Story

RefsHunter● Find references path to global space● Easy to use● No GUI● Memory inefficent 

– more snapshots are not a good idea, really

● Download from the Catalog

Page 35: A Weak Pharo Story
Page 36: A Weak Pharo Story

Avoid memory leaks

● Memory leak tests– Time consuming for basic Pharo image

Page 37: A Weak Pharo Story

#2 – Avoiding Leaks

● Ephemerons are special objects used for finalization

● They do not create leaks by themselves(as the WeakRegistry did)

● Soon in Pharo 6.0

Ephemeron  Finalization

Page 38: A Weak Pharo Story

Ephemerons in Case 3

Ephemeron!

Ephemeron Registry!

Page 39: A Weak Pharo Story

Lessons learned

● Announcements are sometimes overused● Crazy leaking objects in the image

– some tools opened in past during manual integration referenced by active hand click state

● Not every leak lasts forever– it takes 30 seconds to garbage collect closed 

Nautilus

● We need better tools

Page 40: A Weak Pharo Story

The End

● Weak references are nice● But they are not magical

   You can still create memory leaks with them

● Ephemerons will fix it partially    But you still need to know what you're doing a bit...

Page 41: A Weak Pharo Story

Ephemerons – Operational View

● When the GC passes...● 1) It does not traverse ephemerons:

It queues them

● 2) Then

[ traverses ephemerons whose key is referenced ]whileTrue: [

there are ephemerons with keys referenced ]

Page 42: A Weak Pharo Story

Exercises:key

valueephemeron