Memory Management with Java and C++
-
Upload
mohammad-shaker -
Category
Education
-
view
840 -
download
4
Transcript of Memory Management with Java and C++
![Page 1: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/1.jpg)
Understanding
Memory Management
and Garbage Collection
Java and C++
Mohammad Shaker
FIT of Damascus - AI dept.
Programming Languages
![Page 2: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/2.jpg)
What Is A Pointer?
![Page 3: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/3.jpg)
NULL Pointer
![Page 4: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/4.jpg)
Pointer Assignment
![Page 5: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/5.jpg)
Shallow and Deep Copying
![Page 6: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/6.jpg)
Bad Pointers
• When a pointer is first allocated, it does
not have a pointee.
• The pointer is “uninitialized” or simply
"bad".
• every pointer starts out with a bad
value.
• There is nothing automatic that gives
a pointer a valid pointee.
![Page 7: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/7.jpg)
Bad Pointers
IT IS NOT NULL!
![Page 8: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/8.jpg)
Bad Pointers – Faster?!
• Most languages make it easy to omit
this important step.
• The run-time checks are also a reason
why such languages always run at
least a little slower than a compiled
language like C or C++!
![Page 9: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/9.jpg)
![Page 10: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/10.jpg)
![Page 11: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/11.jpg)
![Page 12: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/12.jpg)
Bad Pointers - Example
void BadPointer() {
int* p; // allocate the pointer, but not the pointee
*p = 42; // this dereference is a serious runtime error
}
![Page 13: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/13.jpg)
Pointer Rules Summary
• A pointer stores a reference to its pointee. The
pointee, in turn, stores something useful.
• The dereference operation on a pointer accesses its
pointee. A pointer may only be dereferenced after
it has been assigned to refer to a pointee. Most
pointer bugs involve violating this one rule.
• Allocating a pointer does not automatically assign it
to refer to a pointee. Assigning the pointer to refer
to a specific pointee is a separate operation which
is easy to forget.
• Assignment between two pointers makes them refer
to the same pointee which introduces sharing.
![Page 14: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/14.jpg)
Large Locals Example
void X() {
int a = 1;
int b = 2;
// T1
Y(a);
// T3
Y(b);
// T5
}
void Y(int p) {
int q;
q = p + 2;
// T2 (first time through)
// T4 (second time through)
}
![Page 15: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/15.jpg)
Large Locals Example
![Page 16: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/16.jpg)
A bit harder - Bill Gates By Valuevoid B(int worth) {
worth = worth + 1;
// T2
}
void A() {
int netWorth;
netWorth = 55; // T1
B(netWorth);
// T3 -- B() did not change netWorth
}
![Page 17: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/17.jpg)
A bit harder - Bill Gates By Valuevoid B(int worth) {
worth = worth + 1;
// T2
}
void A() {
int netWorth;
netWorth = 55; // T1
B(netWorth);
// T3 -- B() did not change netWorth
}
![Page 18: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/18.jpg)
A bit harder - Bill Gates By Referencevoid B(int &worth) {
worth = worth + 1;
// T2
}
void A() {
int netWorth;
netWorth = 55; // T1
B(netWorth);
// T3 -- B() did not change netWorth
}
![Page 19: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/19.jpg)
A bit harder - Bill Gates By Referencevoid B(int *worth) {
*worth = *worth + 1;
// T2
}
void A() {
int netWorth;
netWorth = 55; // T1
B(&netWorth);
// T3 -- B() did not change netWorth
}
![Page 20: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/20.jpg)
![Page 21: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/21.jpg)
![Page 22: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/22.jpg)
Heap Memory
![Page 23: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/23.jpg)
Heap!
• "Heap" memory, also known as
"dynamic" memory.
• an alternative to local stack Memory.
![Page 24: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/24.jpg)
Facts
• Local memory is quite automatic — it is allocated automatically on function call and it is deallocated automatically when a function exits.
• Heap Lifetime– Because the programmer now controls
exactly when memory is allocated and deallocated, it is possible to build a data structure in memory, and return that data structure to the caller. This was never possible with local memory which was automatically deallocated when the function exited.
![Page 25: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/25.jpg)
Heap - Allocation
• Allocate 3 GIF images in the heap
each of which takes 1024 bytes of
memory.
![Page 26: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/26.jpg)
Heap - Deallocation
• Deallocating Gif2 image
![Page 27: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/27.jpg)
Simple Heap Example
![Page 28: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/28.jpg)
Simple Heap Example
![Page 29: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/29.jpg)
Simple Heap Example
![Page 30: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/30.jpg)
![Page 31: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/31.jpg)
![Page 32: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/32.jpg)
![Page 33: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/33.jpg)
![Page 34: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/34.jpg)
![Page 35: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/35.jpg)
• both values and the variables are allocated memory. However, each assignment copies into the variable’s block, not the contents of the value block, but instead its address
![Page 36: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/36.jpg)
• both values and the variables are allocated memory. However, each assignment copies into the variable’s block, not the contents of the value block, but instead its address
![Page 37: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/37.jpg)
null.toString();
• we get a NullPointerException (Java)
• we can determine whether the value
of a pointer variable is null or not, and
hence, whether we can access its
member.
![Page 38: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/38.jpg)
• Multiple pointers sharing a value
![Page 39: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/39.jpg)
Point p1 = new ACartesianPoint(50, 50);
Point p2 = p1;
p1.setX(100);
System.out.println(p2.getX());
• When p1 is assigned to p2, the pointer stored
in p1 is copied into p2, not the object itself.
Both variables share the same object, and thus, the
code will print 100 and not 50, as you might expect.
![Page 40: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/40.jpg)
Object Oriented Memory
Management(Java and C++)
![Page 41: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/41.jpg)
Foreknowledge
• A program address space:
– Code area
– Heap (Dynamic Memory Area)
– Execution Stack
![Page 42: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/42.jpg)
Foreknowledge
• Code area– where code to be executed is stored
• Heap– store variables and objects allocated
dynamically
– accessed with no restrictions
• Execution Stack– perform computation
– store local variables
– perform function call management
![Page 43: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/43.jpg)
accessed with a LIFO policy (Last In First Out)
![Page 44: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/44.jpg)
C++ Specific
• C++ has several other memory areas
• C++ the entire code is loaded into
code area, and neglect dynamic
loading.
![Page 45: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/45.jpg)
Activation Record (AR)
void f(){g();
}void g(){
h();}void h(){k();
}
![Page 46: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/46.jpg)
Activation Record (AR)
void f(){g();
}void g(){
h();}void h(){k();
}
![Page 47: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/47.jpg)
Abbreviations for AR
•
![Page 48: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/48.jpg)
Scope Activation Record (SAR)is put every time a new block is encountered
![Page 49: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/49.jpg)
Scope Activation Record (SAR)
• Contains:
– Local variables (declared inside the
block)
– The Static Link SL (a.k.a SAR link) a pointer to the SAR of the
immediate enclosing block; used to acce
ss local variables of outer blocks from the
current block.
![Page 50: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/50.jpg)
Scope Activation Record (SAR)
![Page 51: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/51.jpg)
References and Pointersconceptually the SAME
![Page 52: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/52.jpg)
Classes and ObjectsObjects are instances of classes
![Page 53: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/53.jpg)
Classes and ObjectsObjects are instances of classes
(Objects are classes in action)
![Page 54: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/54.jpg)
C++ again
• In C++ classes are truly user defined ty
pes. Objects are treated as any other
variable and are allocated:
– On the stack, as regular local variables
– On the heap, like in Java
![Page 55: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/55.jpg)
Java example
int[] hello = new int[5];
// reference hello is on stack, the object is on the
// heap.
hello[0] = 2;
// Java puts this value directly in same slot and doesn't // create a wrapping object.
From: http://stackoverflow.com/questions/10820787/how-does-java-treat-primitive-type-arrays
![Page 56: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/56.jpg)
Java: Why are wrapper classes needed?http://stackoverflow.com/questions/2134798/java-why-are-wrapper-classes-needed
![Page 57: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/57.jpg)
Java examplepublic class CoffeeMaker {
public CoffeeMaker(){}
}
..
..
CoffeeMaker aCoffeeMaker;
aCoffeeMaker = new CoffeeMaker();
int sugar = 4;
Integer sugarObject = new Integer(3);
![Page 58: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/58.jpg)
Java examplepublic class CoffeeMaker {
public CoffeeMaker(){}
}
..
..
CoffeeMaker aCoffeeMaker;
aCoffeeMaker = new CoffeeMaker();
int sugar = 4;
Integer sugarObject = new Integer(3);
![Page 59: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/59.jpg)
Java example
• Java objects can be accessed just
through reference variables, that hold
the address of objects in heap.
![Page 60: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/60.jpg)
C++ example
class CoffeeMaker {
public:
CoffeeMaker();
virtual ~CoffeeMaker();
};
// ..
CoffeeMaker aCoffeeMaker;
CoffeeMaker *aPtrCoffeeMaker = new CoffeeMaker();
CoffeeMaker &aRefCoffeeMaker = aCoffeeMaker;
aRefCoffeeMaker = *aPtrCoffeeMaker; // dangerous!
int sugar = 4;
int *ptrSuger = new int;
int &aRefSugar = sugar;
![Page 61: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/61.jpg)
C++ example
![Page 62: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/62.jpg)
Stack and Heap comparison
![Page 63: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/63.jpg)
Issues for objects in memory (Java)
• Objects with no references pointing to
them are considered eligible for
automatic garbage collection by the
system, which runs periodically and
performs the real destruction of the
objects.
• GC is not directly under control of the
programmer.
![Page 64: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/64.jpg)
Issues for objects in memory (C++)
• After an object has been created on
the heap (with the new directive) it
survives until someone destroys it
explicitly using the delete directive.
• This could lead to memory leaks
– if the programmer forgets to delete
objects no longer needed, they remain
on the heap as wasted space
![Page 65: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/65.jpg)
Methods (Java)public class CoffeeMaker {
public void prepareCoffee() {}
public void prepareCoffeeSweet(int sugarAm){}
void main(...) {
CoffeeMaker aCoffeeMaker;
aCoffeeMaker = new CoffeeMaker();
aCoffeeMaker.prepareCoffee();
}
}
![Page 66: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/66.jpg)
Methods (Java)public class CoffeeMaker {
public void prepareCoffee() {}
public void prepareCoffeeSweet(int sugarAm){}
void main(...) {
CoffeeMaker aCoffeeMaker;
aCoffeeMaker = new CoffeeMaker();
aCoffeeMaker.prepareCoffee();
}
}
![Page 67: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/67.jpg)
Methods (C++)class CoffeeMaker {
public:
void prepareCoffee() {}
void prepareCoffeeSweet(int sugarAm){}
void main(...) {
CoffeeMaker *aPtrCoffeeMaker;
aPtrCoffeeMaker = new CoffeeMaker;
aPtrCoffeeMaker-> prepareCoffee();
}
}
![Page 68: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/68.jpg)
Methods (C++)class CoffeeMaker {
public:
void prepareCoffee() {}
void prepareCoffeeSweet(int sugarAm){}
void main(...) {
CoffeeMaker *aPtrCoffeeMaker;
aPtrCoffeeMaker = new CoffeeMaker;
aPtrCoffeeMaker-> prepareCoffee();
}
}
![Page 69: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/69.jpg)
Understanding
Garbage Collection
![Page 70: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/70.jpg)
Read!
• http://www.simple-talk.com/dotnet/.net-framework/understanding-
garbage-collection-in-.net/
• http://en.wikibooks.org/wiki/C_Programming/Memory_management
• (.pdf) Garbage Collection: Automatic Memory Management in the
Microsoft .NET Framework
![Page 71: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/71.jpg)
Dangerous Finalize()!
![Page 72: Memory Management with Java and C++](https://reader033.fdocuments.net/reader033/viewer/2022052900/555e2368d8b42a384f8b4cbe/html5/thumbnails/72.jpg)
Keep goin’