Wszyscy zostaliśmy oszukani! Automatyczne zarządzanie pamięci rozwiąże wszystkie Wasze problemy, mówili. W zarządzanych środowiskach takich jak CLR JVM nie będzie wycieków pamięci, mówili! Właściwie pamięć jest tania i nie musisz się już nią nigdy więcej martwić. Wszyscy kłamali. Automatyczne zarządzanie pamięcią jest wygodną abstrakcją i bardzo często działa dobrze. Ale jak każda abstrakcja, wcześniej czy później "wycieka" ona. I to najczęściej w najmniej spodziewanym i przyjemnym momencie. W tej sesji spróbuję otworzyć oczy na fakt, że błoga nieświadomość nt. tej abstrakcji może być kosztowna. Pokażę jak może się objawić frywolne traktowanie pamięci i co możemy zyskać pisząc kod zdając sobie sprawę, że pamięć jednak nie jest nieskończona, tania i zawsze jednakowo szybka.
18. Hardware - ISA - System -
Framework (JVM, CLR, Go,
…) - Programista
19. int n = 5000;
int m = 5000;
int[,] tab = new int[n, m];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
tab[j, i] = 1;
}
}
int n = 5000;
int m = 5000;
int[,] tab = new int[n, m];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
tab[i, j] = 1;
}
}
20. Method | Size | Mean | Error | StdDev |
--------- |----- |---------------:|--------------:|--------------:|
IJAccess | 50 | 3.350 us | 0.0435 us | 0.0407 us |
JIAccess | 50 | 3.372 us | 0.0491 us | 0.0460 us |
21. Method | Size | Mean | Error | StdDev |
--------- |----- |---------------:|--------------:|--------------:|
IJAccess | 50 | 3.350 us | 0.0435 us | 0.0407 us |
JIAccess | 50 | 3.372 us | 0.0491 us | 0.0460 us |
IJAccess | 500 | 459.295 us | 8.8558 us | 7.3950 us |
JIAccess | 500 | 616.643 us | 11.3232 us | 10.0377 us |
22. Method | Size | Mean | Error | StdDev |
--------- |----- |---------------:|--------------:|--------------:|
IJAccess | 50 | 3.350 us | 0.0435 us | 0.0407 us |
JIAccess | 50 | 3.372 us | 0.0491 us | 0.0460 us |
IJAccess | 500 | 459.295 us | 8.8558 us | 7.3950 us |
JIAccess | 500 | 616.643 us | 11.3232 us | 10.0377 us |
IJAccess | 5000 | 48,728.263 us | 273.4506 us | 255.7859 us |
JIAccess | 5000 | 243,778.158 us | 6,232.9454 us | 5,525.3433 us |
23. public static int[] counters = new int[4];
...
Thread[] workers = new Thread[4];
for (int i = 0; i < 4; ++i)
{
workers[i] = new Thread(idx =>
{
int index = (int)idx;
for (int j = 0; j < 100_000_000; ++j)
{
counters[index] = counters[index] + 1;
}
});
}
31. Method | Mean | Error | StdDev |
-------------- |---------:|----------:|----------:|
DoSharingTest | 925.7 ms | 19.233 ms | 46.816 ms |
DoSharingTest2 | 338.4 ms | 9.131 ms | 26.779 ms |
DoSharingTest3 | 166.8 ms | 1.732 ms | 1.536 ms |
42. [HttpGet]
[Route("values/concatenated/{count}")]
public string GetConcatenated(int count)
{
Random rand = new Random();
string result = string.Empty;
for (int i = 0; i <= count; i++)
{
result += "<Customer Id="";
result += i.ToString();
result += "" lastUpdateDate="";
result += DateTime.Now.ToString();
result += "" branchId="";
result += i.ToString();
result += "" firstName="";
result += i.ToString(); ;
result += "" lastName="";
result += "This is the customer with the Id: ";
result += i.ToString();
result += "" ranking="";
result += rand.Next(100).ToString();
result += ""/>";
}
result = "<Customers>" +
result +
"</Customers>";
return result;
}
[HttpGet]
[Route("values/builder/{count}")]
public string GetBuilder(int count)
{
Random rand = new Random();
StringBuilder sb = new StringBuilder("<Customers>");
for (int i = 0; i <= count; i++)
{
sb.Append("<Customer Id="");
sb.Append(i.ToString());
sb.Append("" lastUpdateDate="");
sb.Append(DateTime.Now.ToString());
sb.Append("" branchId="");
sb.Append(i.ToString());
sb.Append("" firstName="");
sb.Append(i.ToString());
sb.Append("" lastName="");
sb.Append("This is the customer with the Id: ");
sb.Append(i.ToString());
sb.Append("" ranking="");
sb.Append(rand.Next(100).ToString());
sb.Append(""/>");
}
sb.Append("</Customers>");
return sb.ToString();
}
54. From: k...@rational.com (Kent Mitchell)
Subject: Re: Does memory leak?
Date: 1995/03/31
Norman H. Cohen (nco...@watson.ibm.com) wrote:
: The only programs I know of with deliberate memory leaks are those whose
: executions are short enough, and whose target machines have enough
: virtual memory space, that running out of memory is not a concern.
: (This class of programs includes many student programming exercises and
: some simple applets and utilities; it includes few if any embedded or
: safety-critical programs.)
This sparked an interesting memory for me. I was once working with a
customer who was producing on-board software for a missile. In my analysis
of the code, I pointed out that they had a number of problems with storage
leaks. Imagine my surprise when the customers chief software engineer said
"Of course it leaks". He went on to point out that they had calculated the
amount of memory the application would leak in the total possible flight time
for the missile and then doubled that number. They added this much additional
memory to the hardware to "support" the leaks. Since the missile will explode
when it hits its target or at the end of its flight, the ultimate in garbage
collection is performed without programmer intervention.
55. From: k...@rational.com (Kent Mitchell)
Subject: Re: Does memory leak?
Date: 1995/03/31
Norman H. Cohen (nco...@watson.ibm.com) wrote:
: The only programs I know of with deliberate memory leaks are those whose
: executions are short enough, and whose target machines have enough
: virtual memory space, that running out of memory is not a concern.
: (This class of programs includes many student programming exercises and
: some simple applets and utilities; it includes few if any embedded or
: safety-critical programs.)
This sparked an interesting memory for me. I was once working with a
customer who was producing on-board software for a missile. In my analysis
of the code, I pointed out that they had a number of problems with storage
leaks. Imagine my surprise when the customers chief software engineer said
"Of course it leaks". He went on to point out that they had calculated the
amount of memory the application would leak in the total possible flight time
for the missile and then doubled that number. They added this much additional
memory to the hardware to "support" the leaks. Since the missile will explode
when it hits its target or at the end of its flight, the ultimate in garbage
collection is performed without programmer intervention.
56. From: k...@rational.com (Kent Mitchell)
Subject: Re: Does memory leak?
Date: 1995/03/31
Norman H. Cohen (nco...@watson.ibm.com) wrote:
: The only programs I know of with deliberate memory leaks are those whose
: executions are short enough, and whose target machines have enough
: virtual memory space, that running out of memory is not a concern.
: (This class of programs includes many student programming exercises and
: some simple applets and utilities; it includes few if any embedded or
: safety-critical programs.)
This sparked an interesting memory for me. I was once working with a
customer who was producing on-board software for a missile. In my analysis
of the code, I pointed out that they had a number of problems with storage
leaks. Imagine my surprise when the customers chief software engineer said
"Of course it leaks". He went on to point out that they had calculated the
amount of memory the application would leak in the total possible flight time
for the missile and then doubled that number. They added this much additional
memory to the hardware to "support" the leaks. Since the missile will explode
when it hits its target or at the end of its flight, the ultimate in garbage
collection is performed without programmer intervention.
57. From: k...@rational.com (Kent Mitchell)
Subject: Re: Does memory leak?
Date: 1995/03/31
Norman H. Cohen (nco...@watson.ibm.com) wrote:
: The only programs I know of with deliberate memory leaks are those whose
: executions are short enough, and whose target machines have enough
: virtual memory space, that running out of memory is not a concern.
: (This class of programs includes many student programming exercises and
: some simple applets and utilities; it includes few if any embedded or
: safety-critical programs.)
This sparked an interesting memory for me. I was once working with a
customer who was producing on-board software for a missile. In my analysis
of the code, I pointed out that they had a number of problems with storage
leaks. Imagine my surprise when the customers chief software engineer said
"Of course it leaks". He went on to point out that they had calculated the
amount of memory the application would leak in the total possible flight time
for the missile and then doubled that number. They added this much additional
memory to the hardware to "support" the leaks. Since the missile will explode
when it hits its target or at the end of its flight, the ultimate in garbage
collection is performed without programmer intervention.
58. From: k...@rational.com (Kent Mitchell)
Subject: Re: Does memory leak?
Date: 1995/03/31
Norman H. Cohen (nco...@watson.ibm.com) wrote:
: The only programs I know of with deliberate memory leaks are those whose
: executions are short enough, and whose target machines have enough
: virtual memory space, that running out of memory is not a concern.
: (This class of programs includes many student programming exercises and
: some simple applets and utilities; it includes few if any embedded or
: safety-critical programs.)
This sparked an interesting memory for me. I was once working with a
customer who was producing on-board software for a missile. In my analysis
of the code, I pointed out that they had a number of problems with storage
leaks. Imagine my surprise when the customers chief software engineer said
"Of course it leaks". He went on to point out that they had calculated the
amount of memory the application would leak in the total possible flight time
for the missile and then doubled that number. They added this much additional
memory to the hardware to "support" the leaks. Since the missile will explode
when it hits its target or at the end of its flight, the ultimate in garbage
collection is performed without programmer intervention.
59. From: k...@rational.com (Kent Mitchell)
Subject: Re: Does memory leak?
Date: 1995/03/31
Norman H. Cohen (nco...@watson.ibm.com) wrote:
: The only programs I know of with deliberate memory leaks are those whose
: executions are short enough, and whose target machines have enough
: virtual memory space, that running out of memory is not a concern.
: (This class of programs includes many student programming exercises and
: some simple applets and utilities; it includes few if any embedded or
: safety-critical programs.)
This sparked an interesting memory for me. I was once working with a
customer who was producing on-board software for a missile. In my analysis
of the code, I pointed out that they had a number of problems with storage
leaks. Imagine my surprise when the customers chief software engineer said
"Of course it leaks". He went on to point out that they had calculated the
amount of memory the application would leak in the total possible flight time
for the missile and then doubled that number. They added this much additional
memory to the hardware to "support" the leaks. Since the missile will explode
when it hits its target or at the end of its flight, the ultimate in garbage
collection is performed without programmer intervention.
60. From: k...@rational.com (Kent Mitchell)
Subject: Re: Does memory leak?
Date: 1995/03/31
Norman H. Cohen (nco...@watson.ibm.com) wrote:
: The only programs I know of with deliberate memory leaks are those whose
: executions are short enough, and whose target machines have enough
: virtual memory space, that running out of memory is not a concern.
: (This class of programs includes many student programming exercises and
: some simple applets and utilities; it includes few if any embedded or
: safety-critical programs.)
This sparked an interesting memory for me. I was once working with a
customer who was producing on-board software for a missile. In my analysis
of the code, I pointed out that they had a number of problems with storage
leaks. Imagine my surprise when the customers chief software engineer said
"Of course it leaks". He went on to point out that they had calculated the
amount of memory the application would leak in the total possible flight time
for the missile and then doubled that number. They added this much additional
memory to the hardware to "support" the leaks. Since the missile will explode
when it hits its target or at the end of its flight, the ultimate in garbage
collection is performed without programmer intervention.
67. let s1 = String::from("hello");
let s2 = s1;
println!("{}, world!", s1);
error[E0382]: use of moved value: `s1`
--> src/main.rs:5:28
|
3 | let s2 = s1;
| -- value moved here
4 |
5 | println!("{}, world!", s1);
| ^^ value used here after move
|
= note: move occurs because `s1` has type `std::string::String`, which does
not implement the `Copy` trait
68. fn main() {
let s = String::from("hello"); // s comes into scope
takes_ownership(s); // s's value moves into the function...
// ... and so is no longer valid here
}
fn takes_ownership(some_string: String) { // some_string comes into scope
println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
// memory is freed.
69. fn main() {
let s = String::from("hello");
use(&s);
}
fn use(some_string: &String) {
// …
}
70. fn main() {
let s = String::from("hello");
change(&s);
}
fn change(some_string: &String) {
some_string.push_str(", world");
}
error[E0596]: cannot borrow immutable borrowed content `*some_string` as mutable
--> error.rs:8:5
|
7 | fn change(some_string: &String) {
| ------- use `&mut String` here to make mutable
8 | some_string.push_str(", world");
| ^^^^^^^^^^^ cannot borrow as mutable
71. fn main() {
let mut s = String::from("hello");
change(&mut s);
}
fn change(some_string: &mut String) {
some_string.push_str(", world");
}
72. let mut s = String::from("hello");
let r1 = &mut s;
let r2 = &mut s;
println!("{}, {}", r1, r2);
error[E0499]: cannot borrow `s` as mutable more than once at a time
--> src/main.rs:5:14
|
4 | let r1 = &mut s;
| ------ first mutable borrow occurs here
5 | let r2 = &mut s;
| ^^^^^^ second mutable borrow occurs here
6 |
7 | println!("{}, {}", r1, r2);
| -- first borrow later used here
73. let mut s = String::from("hello");
let r1 = &s; // no problem
let r2 = &s; // no problem
let r3 = &mut s; // BIG PROBLEM
println!("{}, {}, and {}", r1, r2, r3);
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as
immutable
--> src/main.rs:6:14
|
4 | let r1 = &s; // no problem
| -- immutable borrow occurs here
5 | let r2 = &s; // no problem
6 | let r3 = &mut s; // BIG PROBLEM
| ^^^^^^ mutable borrow occurs here
7 |
8 | println!("{}, {}, and {}", r1, r2, r3);
| -- immutable borrow later used here
- Story o dodaniu pola w obiekcie w grze na PS4 - kto ostatnio myślał o pamięci?
- Jest gdzieś inny świat, w którym takimi rzeczami trzeba się martwić.
Jak wyżej, ale zakończyć tą zabawę i mówić dalej:
zielony: moc komputerów, w tym dostępność pamięci. Zaczynaliśmy od bajtów, teraz nawet smartwatch ma 4GB
czerwony: wraz z mocą komputerów maleje nasze zrozumienie i dobre ich wykorzystanie
PC z 4MB, kto bawił się w konfiguracje ustawień w autoexec.bat oraz config.sys, wyładowywanie sterowników od SB (Sound Blaster) w jednej oraz przydzielane dodatkowych kilobajtów (tak kilobajtów!) pamięci XMS czy DMA
że to refleksja o pamięci, ale że celem jest oczywiście, by wynieśli coś ciekawego i praktycznego
Jak wyżej, ale zakończyć tą zabawę i mówić dalej:
zielony: moc komputerów, w tym dostępność pamięci. Zaczynaliśmy od bajtów, teraz nawet smartwatch ma 4GB
czerwony: wraz z mocą komputerów maleje nasze zrozumienie i dobre ich wykorzystanie
PC z 4MB, kto bawił się w konfiguracje ustawień w autoexec.bat oraz config.sys, wyładowywanie sterowników od SB (Sound Blaster) w jednej oraz przydzielane dodatkowych kilobajtów (tak kilobajtów!) pamięci XMS czy DMA
- Witamy w pracy z Mark I/ENIAC!
Mark I
16 metrów długości, 2.5 metra wysokości
kto zgadnie ile pamięci mieściła? 72 rejestry 23 znakowe (78-bitowe) zatem oferowała pamięć rzędu 702 bajtów RAMU, brak dysków
ENIAC
-
Nie ma tu abstrakcji, programujemy na żywym metalu i jesteśmy świadomi każdego rejestru, każdego bitu/cyfry bawiąc się kabelkami i guzikami.
Teraz oczywiście bardzo to się zmieniło, ale tamto było tylko 70 lat temu!
Co tu przecieka? :D
[Slajd ze schematem współczesnego CPU, L1/L2/L3 cache, RAM (Random Access Memory)]
- Wspomnieć o L4 dostępnych przez chwilę w Broadwellu
- Jesteśmy szczęśliwymi (?) programistami assemblera i nie przejmujemy się niczym - mamy abstrakcję pamięci i instrukcji na nich operujących
- Przy okazji - odnieść się do takiego samego wykresu krążącego po Internecie.
Jeff Dean z Google na podstawie artykułu Petera Norviga z 2002
- Bardzo istotne dla wydajności staje się jak używa pamięci
Tu zatrzymamy się na chwilę, bo to nasza codzienność.
Czy framework (CLR?) przykrywa poprzednio pokazany problem z memory access?
Diagnostic – Intel VTune
Diagnostic – Intel VTune
Diagnostic – Intel VTune
Demo Bartka Adamczewskiego:
- zly “access patern” poprzez bezsensowne wczytywanie 64B cache lines by tylko odczytać jeden int,
- CPU nie ma szans wykorzystać żadnego prefetchingu
- Cache hit/miss - kto to obserwuje np. performance counterami?
Diagnostic – Intel VTune
Diagnostic – Intel VTune
Jakie tu demo?
Jakie tu demo?
Na początku deploy w piątek o 18, potem sobie działa działa i po północy jestem na imprezie a oni dzwonią, że PRD przestał działać.
To NIE fajny wyciek abstrakcji.
Miał na myśli, że zrozumienie jak działa samochód i co tam jest “pod spodem” czyni Cię lepszym kierowcą rajdowym.
Kierowca pracujący w harmonii z samochodem by wykorzystać go jak najlepiej.
Powiedzenie zaaplikowane do IT przez Martin Thompson.