3. public void CaffeineCheck(int coffeesConsumed)
{
int coffeesRequired = 3;
CaffeineAlert alert = new CaffeineAlert();
if (coffeesConsumed < coffeesRequired)
{
alert.Announce("Send help.");
}
}
4. The Stack
CaffeineCheck (int coffeesRequired)
int coffeesRequired
int coffeesConsumed
.....
[Return address]
Alert()
CaffeineCheck()
ParentMethod()
For each thread
6. public void CaffeineCheck(int coffeesConsumed)
{
int coffeesRequired = 3;
CaffeineAlert alert = new CaffeineAlert();
if (coffeesConsumed < coffeesRequired)
{
CaffeineCheck(int coffeesRequired)
int coffeesRequired
int coffeesConsumed
CaffeineAlert alert
[Return address]
Stack
CaffeineAlert
alert
alert.Announce("Send help.");
}
}
7. string
Email
string
Name
Contact
list
string
Email
string
Name
Contact
list
CaffeineAlert
alert
public class CaffeineAlert
{
private List<Contact> Recipients;
CaffeineCheck(int coffeesRequired)
int coffeesRequired
int coffeesConsumed
CaffeineAlert alert
[Return address]
Stack
}
public class Contact
{
private string Name;
private string Email;
}
List<Contact>
Recipients
8. public class CaffeineAlert
{
private List<Contact> Recipients;
CaffeineCheck(int coffeesRequired)
int coffeesRequired
int coffeesConsumed
CaffeineAlert alert
[Return address]
}
public class Contact
{
private string Name;
private string Email;
}
WRONG
Stack Heap
9. public class CaffeineAlert
{
private List<Contact> Recipients;
CaffeineCheck(int coffeesRequired)
int coffeesRequired
int coffeesConsumed
CaffeineAlert alert
[Return address]
}
public class Contact
{
private string Name;
private string Email;
}
WRONG
Stack Heap
10. public class CaffeineAlert
{
private List<Contact> Recipients;
CaffeineCheck(int coffeesRequired)
int coffeesRequired
int coffeesConsumed
CaffeineAlert alert
[Return address]
}
public class Contact
{
private string Name;
private string Email;
}
WRONG
Stack Heap
11. public class CaffeineAlert
{
private List<Contact> Recipients;
CaffeineCheck(int coffeesRequired)
int coffeesRequired
int coffeesConsumed
CaffeineAlert alert
[Return address]
}
public class Contact
{
private string Name;
private string Email;
}
WRONG
Stack Heap
12. string
Email
string
Name
Contact
list
string
Email
string
Name
Contact
list
CaffeineAlert
alert
public class CaffeineAlert
{
private List<Contact> Recipients;
CaffeineCheck(int coffeesRequired)
int coffeesRequired
int coffeesConsumed
CaffeineAlert alert
[Return address]
Stack
}
public class Contact
{
private string Name;
private string Email;
}
List<Contact>
Recipients
34. public class UnmanagedClass : IDisposable
{
public void Dispose()
{
//Clear up the unmanaged resources
GC.SuppressFinalize(this);
}
~UnmanagedClass()
{
this.Dispose();
}
} Not a complete IDisposable implementation
If you’re implementing using this, read http://stackoverflow.com/a/538238/1326403
http://www.red-gate.com/memoryprofiler
Warm up the application
Take a baseline snapshot
Run a small reproduction
Take another snapshot
Identify unexpected objects
Eliminate cause for objects
Repeat!
Also worth reading: https://www.simple-talk.com/books/.net-books/under-the-hood-of-.net-memory-management/