Control - Procedures and Environments Control Procedure definition - - PowerPoint PPT Presentation
Control - Procedures and Environments Control Procedure definition - - PowerPoint PPT Presentation
Control - Procedures and Environments Control Procedure definition and activation: A procedure is a mechanism in a programming language for abstracting a group of actions or computations. The group of actions is called the body of the
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
A procedure is a mechanism in a programming language for
abstracting a group of actions or computations.
The group of actions is called the body of the procedure. A procedure is represented by a specification including
A name,
The types and names of parameters.
The type of the return value.
We shall not make a significant distinction between a function
and a procedure, although differences exist.
2
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
Example:
//C++ code void intSwap (int &x, int &y) //Specification { int temp = x; //Body x = y; //Body y = temp; //Body }
Formal Parameters
3
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
A procedure is called or activated by stating its
name, together with arguments to the call corresponding to the parameters.
Example:
intSwap(a, b);
Actual Parameters
4
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
A call to the procedure transfers control to the
beginning of the body of the called procedure (The Callee).
In some languages, control can be returned to the
caller even before reaching the end of the callee’s body by using a return statement.
5
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
Example:
//C++ code void intSwap (int &x, int &y) { if (x == y) return; int temp = x; x = y; y = temp; }
If x is equal to y, the function will exit here
6
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
In some languages such as FORTRAN, to call a
procedure, one must also include the keyword CALL.
CALL INTSWAP (A, B)
In FORTRAN, procedures are called subroutines.
7
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
In some languages, procedure and function declarations
are written in a form similar to constant declarations:
Example: ML
(* ML code *) fun swap (x, y) = let val t = !x in x:= !y y:= t end;
8
- Dr. Sherif G. Aly
Control
Procedure definition and activation:
In such ML case, we can say that a procedure declaration
creates a constant procedure value.
It associates a symbolic name (the name of the procedure)
with the value.
A procedure communicates with the rest of the program
through its:
Parameters
Non-local references (references to variables outside the procedure body)
9
- Dr. Sherif G. Aly
Control
Procedure Semantics:
When a block is encountered during execution, it
causes the allocation of local variables and other
- bjects corresponding to the declarations of the
block.
The memory allocated for the local objects of the
block is called the activation record (stack frame).
10
- Dr. Sherif G. Aly
Control
Procedure Semantics:
When a block is encountered during execution, it
causes the allocation of local variables and other
- bjects corresponding to the declarations of the
block.
The memory allocated for the local objects of the
block is called the activation record (stack frame).
11
- Dr. Sherif G. Aly
Control
Procedure Semantics:
int x; void B(void){ int i; i = x/2; } void A(void){ int x, y; B(); } main(){ A(); return 0; }
x x y i
Global Environment Activation Record of A Activation Record of B Using Lexical Scoping, which x is this?
12
- Dr. Sherif G. Aly
Control
Procedure Semantics:
int x; void B(void){ int i; i = x/2; } void A(void){ int x, y; B(); } main(){ A(); return 0; }
x x y i
Global Environment Activation Record of A Activation Record of B Using Lexical Scoping, which x is this?
The global environment is called the defining environment of B The activation record of A is called the calling environment of B
13
- Dr. Sherif G. Aly
Control
Procedure Semantics:
int x; void B(void){ int i; i = x/2; } void A(void){ int x, y; B(); } main(){ A(); return 0; }
x x y i
Global Environment Activation Record of A Activation Record of B Using Lexical Scoping, which x is this?
The global environment is called the defining environment of B The activation record of A is called the calling environment of B The x is that of the defining environment : The global x
14
- Dr. Sherif G. Aly
Control
Procedure Semantics:
A procedure may have multiple calling
environments (could be called from more than one place).
However, a procedure will have one defining
environment.
15
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by value. Pass by reference. Pass by value result. Pass by name and delayed evaluation.
16
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by value:
The value of the parameter is evaluated.
The parameter is passed as a constant.
It’s value cannot be modified, or if modified does not affect the actual parameter.
This is by far the most common mechanism for parameter passing.
In Java, all primitive data types are passed by value.
17
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by value (C):
void increment (int x){ x++; } increment (y); A copy of y is made and passed to x. Changing x DOES NOT change y.
18
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by reference:
The location of the parameter is passed.
The formal parameter becomes an alias to the actual parameter.
Any change in the formal parameter affects the actual parameter.
In FORTRAN, passing by reference is the only allowed parameter passing mechanism.
In C++ and Pascal, passing by reference is specified using an extra syntax.
19
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by reference (C):
void increment (int &x){ x++; } increment (y); x becomes an alias of y. Changing x affects y. The & before x specifies passing by reference.
20
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by reference:
If FORTRAN only allows passing by reference. How is a call such as inc(2) achieved? A temporary variable is located, initialized with 2, and
passed.
This mimics passing by value in a passing by reference
mechanism.
21
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by Value-Result:
This mechanism achieves a similar result to passing by reference.
No actual alias is established.
A copy of the actual parameter is made, used in the procedure, and then copied back to the actual parameter.
Also known as copy-in, copy-out.
Also known as copy-restore.
22
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by Name and Delayed Evaluation:
The actual parameter passed to the procedure is NOT
evaluated until it is actually used in the called procedure.
23
- Dr. Sherif G. Aly
Control
Parameter Passing Mechanisms:
Pass by Name and Delayed Evaluation:
Example:
Void inc(int x) { … x++; } inc(a[i]);
a[i] only evaluated here! If i somehow changed in procedure inc before this point, BIG problem!! We will not be incrementing the actually intended a[i]
24
- Dr. Sherif G. Aly
Control
Parameter Passing Specification:
Parameter passing specification is different than
parameter passing mechanisms.
Parameter passing mechanisms are tied closely
to the internal mechanics of the code used to implement them.
25
- Dr. Sherif G. Aly
Control
Parameter Passing Specification:
In Ada for example, we can specify parameters as
In:
Cannot be legally assigned a new value inside the procedure, or otherwise have its value changed!
More like a constant.
Out:
Can only be assigned to.
Its value can never be used.
In Out :
Both
The meaning of such words is exactly what we can expect. Any programs violating the rules above is considered erroneous! 26
- Dr. Sherif G. Aly
Control
Type Checking of Parameters:
In strongly typed languages:
Procedure calls must be checked so that the actual
parameters agree in type and number with the formal parameters.
Procedures may not have a variable number of
parameters.
Rules must be stated for type compatibility.
27
- Dr. Sherif G. Aly
Control
Procedure Environments, Activations, and
Allocation:
In block structured language with recursion, such as C and
Algol like languages, we need a stack to store various scope information.
In stack based runtime environments:
An environment pointer is needed to point to the current activation.
A control link is needed to point to the previous activation record of the block from which control passed to the current block and to which control will return.
28
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
In an imperative language such as C, the automatic
allocation and deallocation of storage occurs only for activation records on the stack.
Space is allocated for an activation record on the stack
when a procedure is called, and deallocated when the procedure exits.
Explicit dynamic allocation and use of pointers is also
available under manual programmer control using a heap
- f memory separate from the stack.
29
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
However, the use of the heap has many problems
including:
Creation of garbage. Dangling references.
Languages with significant needs of heap storage
such as Java leave non-stack dynamic storage to a memory manager that provides automatic garbage collection.
30
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Automated memory management falls into two
categories:
Garbage collection: the reclamation of previously
allocated but no longer used storage.
The maintenance of the free space available for
allocation.
31
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Maintaining free space:
A contiguous block of memory is usually provided by the
- perating system for use of an executing program.
The free space is maintained by a list of free blocks.
When a block of certain size needs to be allocated, the memory manager searches the list of free blocks for a free block with a large enough space.
If a block is found, it is allocated, and the list of free blocks is updated.
32
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Maintaining free space:
When a block is freed, such blocks are also returned to
the list of free blocks.
When blocks are freed, they must be joined with
immediately adjacent blocks to form the largest contiguous block of free memory. This is called coalescing.
33
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Maintaining free space:
However, even with joining adjacent blocks to form
larger blocks, because there is no one single large block, it is possible for a large allocation to fail, even though there is enough total space in smaller scattered blocks!
Memory must occasionally be compacted by moving all
free blocks together and coalescing them into one block.
34
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Maintaining free space: Combine them into one block: coalescing Two adjacent free blocks Compact all free space together
35
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Maintaining free space:
Coalescing: combine two adjacent free blocks to form
- ne single larger block.
Compaction: move all free space together from around
memory to form the largest possible free space block.
Compaction also enhances performance.
36
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Reclamation of Storage:
Recognizes when a block of storage is no longer
referenced, either directly or indirectly through pointers.
This is a much more difficult task than the maintenance
- f free lists.
37
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Reclamation of Storage:
Two main methods have been used: Reference counting Mark and Sweep
38
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Reference Counting:
Tries to reclaim space as soon as it is no longer
referenced.
Each block of allocated storage contains an extra count
field that stores the number of references to this block from
- ther blocks.
When the reference count is zero, the block is freed, but be
careful, the block itself may contain references to others, so indeed it may not be freed also!
39
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Drawbacks of Reference Counting:
Memory space is wasted to store the counter Maintaining the counters is a significant overhead!
40
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Mark and Sweep:
Is a lazy method.
It puts off reclaiming any storage until the allocator runs out of space.
It looks for all storage that can be referenced and moves all unreferenced storage back to the free space.
This is performed using two passes.
41
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Mark and Sweep:
First pass: Follows all pointers recursively, starting with the current
environment or symbol table, and marks each block of storage reached.
Requires a bit of storage for marking. Second pass: Sweeps linearly through memory, returning unmarked
blocks to the free list.
42
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Drawbacks of Mark and Sweep:
An extra bit of storage is needed. The two passes through memory causes a significant
delay in processing each time the garbage collector is invoked: could be a few seconds every couple of minutes!
This delay may be very unacceptable in interactive
applications requiring immediate response.
43
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Enhancement of Mark and Sweep (Stop and Copy):
Memory can be split into two halves.
Memory is allocated from one half at a time.
During the marking pass, all reached blocks that should be marked are immediately copied to the unused half.
No extra bit for marking is needed !
This is called stop and copy.
44
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Enhancement of Mark and Sweep (Stop and
Copy):
Once all reached blocks are copied, the used and
unused halves are interchanged.
Compaction is therefore done automatically as such. However, copying is still an overhead!
45
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Generational Garbage Collection:
Invented in the 1980’s and significantly reduces
processing delays.
A permanent storage area is made available. Allocated objects that survive long enough are simply
copied to the permanent space, and NEVER deallocated during subsequent storage reclamations.
46
- Dr. Sherif G. Aly
Control
Dynamic Memory Management:
Generational Garbage Collection:
The garbage collector only needs to search only a very
small section of memory for newer storage allocations.
It is still possible for the permanent memory to become
exhausted.
However, this process has proven to work very well.
47
Garbage Collection in Java
Contribution by Ethan Henry – Sitraka http://java.quest.com/
- Dr. Sherif G. Aly
What is a JVM?
The Java Virtual Machine has 2 primary jobs:
Execute Code Manage Memory
It also does stuff like managing locks Most other functions are really not part of the
JVM, but the library, like file & network I/O
49
- Dr. Sherif G. Aly
Memory Management
Allocate Memory from OS Manage Java Allocations
including heap compaction
Remove Garbage Objects
Java Virtual Machine Specification 2.4.6
50
- Dr. Sherif G. Aly
What is Garbage Anyhow?
“An object is created in the heap and is garbage- collected after there are no more references to it. Objects cannot be reclaimed or freed by explicit language directives.”
Objects become garbage when there are no
more references to the object
This is not to suggest that garbage collectors
have to be implemented using reference counting however...
51
- Dr. Sherif G. Aly
Reachability
Objects become garbage when they’re no
longer reachable from the root set
The root set consists of:
static reference fields in classes local references
Exact versus Non-Exact Garbage Collectors
52
- Dr. Sherif G. Aly
Root Set Heap
Reachable Objects
Elements within the root set directly refer to objects within the
heap of the JVM
Reference variables within those objects refer to further objects
within the Heap (indirectly reachable from the Root Set)
53
- Dr. Sherif G. Aly
How is it implemented?
Sun Classic Sun HotSpot IBM
54
- Dr. Sherif G. Aly
Sun Classic
Mark, Sweep & Compact
Mark: identify garbage Sweep: Find garbage on heap, de-allocate it Compact: collect all empty memory together
Eligibility for garbage collection is determined
by walking across memory, determining reachability and then compacting the heap
Compaction is just copying the live objects so
that they’re adjacent in memory
there’s one large, contiguous block of free memory 55
- Dr. Sherif G. Aly
The Pause
The main problem with classic mark, sweep
and compact is that all other threads have to be suspended while the garbage collector runs
Pause time is proportional to the number of
- bjects on the heap
not the amount of garbage
56
- Dr. Sherif G. Aly
Sun HotSpot
Sun improved memory management in the
Java 2 VMs (JDK 1.2 and on) by switching to a generational garbage collection scheme
The heap is separated into two regions:
New Objects Old Objects
57
- Dr. Sherif G. Aly
New Object Region
The idea is to use a very fast allocation
mechanism and hope that objects all become garbage before you have to garbage collect
The New Object Regions is subdivided into
three smaller regions:
Eden, where objects are allocated 2 “Survivor” semi-spaces: “From” and “To”
58
- Dr. Sherif G. Aly
New Object Region
The Eden area is set up like a stack - an
- bject allocation is implemented as a pointer
increment
When the Eden area is full, the GC does a
reachability test and then copies all the live
- bjects from Eden to the “To” region
The labels on the regions are swapped
“To” becomes “From” - now the “From” area has
- bjects
59
- Dr. Sherif G. Aly
New Object Region
The next time Eden fills objects are copied
from both the “From” region and Eden to the “To” area
There’s a “Tenuring Threshold” that
determines how many times an object can be copied between survivor spaces before it’s moved to the Old Object region
Note that one side-effect is that one survivor
space is always empty
60
- Dr. Sherif G. Aly
Old Object Region
The old object region is for objects that will
have a long lifetime
The hope is that because most garbage is
generated by short-lived objects that you won’t need to GC the old object region very
- ften
61
- Dr. Sherif G. Aly
Generational Garbage Collection
Eden SS1 SS2 Old Eden SS1 SS2 Old Eden SS1 SS2 Old Eden SS1 SS2 Old
First GC Second GC
Eden SS1 SS2 Old
New Object Region Old Object Region
62
- Dr. Sherif G. Aly
GC Output
Running the JVM with -verbosegc will show
information like this:
[GC 1667K->1295K(1984K), 0.0101756 secs] [GC 1807K->1434K(1984K), 0.0223998 secs] [GC 1946K->1574K(2112K), 0.0116185 secs] [Full GC 1574K->1574K(2112K), 0.0830561 secs] [GC 3454K->2081K(4672K), 0.0495951 secs] [GC 4001K->2599K(4672K), 0.0274256 secs] [GC 4519K->3101K(5056K), 0.0308995 secs] [Full GC 3101K->3101K(5056K), 0.1452472 secs] [GC 7039K->4131K(9452K), 0.0777414 secs] [GC 8227K->5174K(9452K), 0.0627538 secs] [GC 9270K->6209K(10348K), 0.1125570 secs]
63
- Dr. Sherif G. Aly
Incremental Garbage Collection
-Xincgc Sun also has an incremental collector that
breaks that old-object region into smaller chunks and GCs them individually
Pause times are smaller but overall
throughput is decreased
[Inc GC 3566K->3950K(5120K), 0.0309922 secs] [GC 4078K->3594K(5184K), 0.0264542 secs] [Inc GC 3594K->3978K(5120K), 0.0272683 secs] [GC 4106K->3627K(5120K), 0.0272381 secs] [Inc GC 3627K->4011K(5056K), 0.0285464 secs] [GC 4139K->3666K(5184K), 0.0281388 secs]
64
- Dr. Sherif G. Aly
Concurrent Garbage Collection
-Xconcgc Concurrent GC allows other threads to keep
running in parallel with the GC
Available in JDK 1.4.1 [GC 1463K->1093K(2560K), 0.0089573 secs] [GC 1093K(2560K), 0.0053470 secs] [GC 1094K(2560K), 0.0092867 secs] [GC 1604K->1228K(2560K), 0.0104823 secs] [GC 1228K(2560K), 0.0062662 secs] [GC 1234K(2560K), 0.0097820 secs] [GC 1740K->1373K(2560K), 0.0115875 secs]
65
- Dr. Sherif G. Aly
Parallel GC
Future JVMs will include the ability to run GC
- n separate processors on a multi-processor
machine
This is already available in BEA’s JRockit
JVM
http://www.jrockit.com
66
- Dr. Sherif G. Aly
IBM
Improved single-heap mark, sweep &
compact
Uses parallel marking in JDK 1.3.0 and
concurrent marking in JDK 1.3.1
IBM’s JVM provides a much more detailed
breakdown of what’s happening during GC
67
- Dr. Sherif G. Aly
IBM GC
After the IBM JVM GC has identified garbage
it tries to free memory for the new allocation by
compacting allocated memory finalizing pending dead objects removing weakly reachable objects increasing the heap space for more on weak refs, see
http://developer.java.sun.com/developer/technicalArticles/ALT/Re fObj/
68
- Dr. Sherif G. Aly
IBM GC
<AF[9]: Allocation Failure. need 2064 bytes, 4096 ms since last AF> <AF[9]: managing allocation failure, action=1 (0/16086056) (34768/34768)> <GC: Tue Sep 03 18:07:42 2002 <GC(9): freed 2345744 bytes in 234 ms, 14% free (2380512/16120824)> <GC(9): mark: 226 ms, sweep: 8 ms, compact: 0 ms> <GC(9): refs: soft 0 (age >= 1), weak 0, final 0, phantom 0> <AF[9]: managing allocation failure, action=3 (2380512/16120824)> <GC(9): need to expand mark bits for 19659768-byte heap> <GC(9): expanded mark bits by 53248 to 307200 bytes> <GC(9): need to expand alloc bits for 19659768-byte heap> <GC(9): expanded alloc bits by 53248 to 307200 bytes> <GC(9): expanded heap by 3538944 to 19659768 bytes, 30% free> <AF[9]: completed in 2974 ms>
69
- Dr. Sherif G. Aly
IBM GC
<AF[20]: Allocation Failure. need 24 bytes, 29793 ms since last AF> <AF[20]: managing allocation failure, action=1 (0/37167760) (1956200/1956200)> <GC: Tue Sep 03 18:09:36 2002 <GC(20): freed 20688256 bytes in 467 ms, 57% free (22644456/39123960)> <GC(20): mark: 453 ms, sweep: 14 ms, compact: 0 ms> <GC(20): refs: soft 0 (age >= 6), weak 0, final 41, phantom 0> <GC(20): stop threads time: 405, start threads time: 56> <AF[20]: completed in 961 ms>
70
- Dr. Sherif G. Aly
How To Tune GC
Not a simple topic There are no universal magic values - every
app is different
Things to tune:
Memory Size
overall size, individual region sizes
GC parameters
Minimum/maximum % of free heap,
Type of GC - single heap, generational,
incremental, concurrent, parallel
71
- Dr. Sherif G. Aly
Tuning Parameters - Both
- ms, -Xms
sets the initial heap size
- mx, -Xmx
sets the maximum heap size
- Xss
sets the size of the per-thread stacks
- Xminf [0-1], -XX:MinHeapFreeRatio [0-100]
sets the percentage of minimum free heap space - controls heap expansion rate
- Xmaxf [0-1], -XX:MaxHeapFreeRatio [0-100]
sets the percentage of maximum free heap space - controls when the VM will return unused heap memory to the OS
72
- Dr. Sherif G. Aly
Tuning Parameters - Sun
- XX:NewRatio
sets the ratio of the old and new generations in the heap. A NewRatio of 5 sets the ratio of new to old at 1:5, making the new generation occupy 1/6th of the overall heap defaults: client 8, server 2
- XX:NewSize, -XX:MaxNewSize [1.3]
- Xmn [1.4]
sets the minimum and maximum sizes of the new object area,
- verriding the default calculated by the NewRatio
- XX:SurvivorRatio
sets the ratio of the survivor space to the eden in the new object
- area. A SurvivorRatio of 6 sets the ratio of the three spaces to
1:1:6, making each survivor space 1/8th of the new object region default: 25
73
- Dr. Sherif G. Aly
Tuning Parameters - Sun
- XX:+AggressiveHeap
This option instructs the JVM to push memory use to the limit: the overall heap is around 3850MB, the memory management policy defers collection as long as possible, and (in some VMs) some GC activity is done in parallel. Because this option sets heap size, do not use in conjunction with the -Xms or -Xmx
- ptions
74
- Dr. Sherif G. Aly
Tuning Parameters - IBM
-Xgcpolicy:<optthruput | optavgpause>
Setting gcpolicy to optthruput disables concurrent mark.
Users who do not have pause time problems (as seen by erratic application response times) should get the best throughput with this option. Optthruput is the default setting.
Setting gcpolicy to optavgpause enables concurrent mark
with its default values. Users who are having problems with erratic application response times caused by normal garbage collections can alleviate those problems at the cost of some throughput when running with the
- ptavgpause option.
75
- Dr. Sherif G. Aly
http://java.sun.com/docs/hotspot/gc http://java.sun.com/docs/performance http://www-
106.ibm.com/developerworks/library/j- jtc/index.html
76
- Dr. Sherif G. Aly
Loitering Objects
What won’t the garbage collector clean up?
77
- Dr. Sherif G. Aly
Loiterers
Allocated Reachable Live “Loiterer” Handled by GC
78
- Dr. Sherif G. Aly
Finding Loiterers
How do you identify these loitering objects? You need to
identify specific use cases for your application that
are important or that you suspect have problems
get a tool that lets you inspect what’s on the heap
before and after each use case
look at the difference - are those the objects you
expect to see created?
are any unnecessary objects still being
referenced?
79
- Dr. Sherif G. Aly
Tools
See some reviews on
http://www.javaperformancetuning.com/tools/index.s html
80
- Dr. Sherif G. Aly
Fixing Loiterers
Track down what objects are loitering Track the instances to their allocation point Decide when they should become
unreachable and make sure it actually happens
81
- Dr. Sherif G. Aly
End
82