Professional Documents
Culture Documents
C Programming Language “
Vijayawada
Presented by
Email:shri136@gmail.com Email:sekhar_chandra64@yahoo.com
ABSTRACT INTRODUCTION
Garbage collection absolves the C is and has always been one of the
developer from tracking memory usage and most widely and popularly used programming
knowing when to free memory. If unreferenced languages in the world. We, as a part of our B.E
locations are not collected, they pollute the Degree course have done a lot of programming
memory and may lead to exhaustion of available in C and have always used the built in functions
memory during the execution of the program. defined in the standard library like “malloc”,
Garbage collection (GC) is an important part of “calloc” etc. to allocate memory and when the
many modern language implementations. The allocated memory is not needed any more we use
benefits of garbage collection in comparison the built in function “free” to free up the
with manual memory management techniques allocated memory. What we have attempted to
are well known to programmers who have used improve the functionality of the ‘C’
high-level languages like Java and C#. Software programming language by developing a library
professionals estimate that the programming that supports garbage collection. We were
effort required to manually perform dynamic inspired by the presence of the GC features in
memory management is approximately 40% of JAVA and proceeded to develop one for C. So
the total cost of developing a large software now, the ‘C’ programmer is relived of the task of
system. Unfortunately, these benefits have not keeping track of what memory locations are
traditionally been available to developers in ‘C’ currently being referenced and what are not. If
programming language because of the lack of the programmer uses our library for allocating
support for the GC feature. memory dynamically, we will make sure
(conservatively) that the allocated memory is
automatically freed (i.e.) garbage is collected
when it is no longer referenced.
Myths about GC
We have developed a library that builds
in this feature, into the ‘C’ programming GC is necessarily slower than manual
language and thus improving its functionality. memory management.
We have in our library a set of routines that GC will necessarily make my program
embed the feature of automatic dynamic memory pause.
management in to the ‘C’ programming Manual memory management won't
language. These routines defined in our library cause pauses.
form, the programmer’s Interface to our garbage GC is incompatible with C.
collector. The interface is simple and the syntax
is very much similar to that of the one already Facts about GC
available in the standard library (stdlib.h) with
very little modifications. In addition to the Most allocated objects are dynamically
routines that support GC we also have routines referenced by a very small number of
that support Finalizers amongst others. Pointers.
The most important small number is
ONE.
Most allocated objects have short
lifetimes.
Allocation patterns (size distributions,
lifetime distributions) are bursty, not
uniform.
Optimal" strategies can fail miserably.
Garbage Collector and
Finalizers
A finalizer is some code that runs when Some garbage collectors, however, may
an object is about to be freed. One of the main choose not to distinguish between genuine object
uses of destructors is to perform clean up references and look-alikes. Such garbage
activities. A couple of typical uses are listed collectors are called conservative because they
below as follows, may not always free every unreferenced object.
One use is for objects that have a state Sometimes a garbage object will be wrongly
outside the program itself. The canonical considered to be live by a conservative collector,
example is an object that refers to a file. When a because an object reference look alike referred to
file object becomes eligible for reclamation, the it. Conservative collectors trade-off an increase
garbage collector needs to ensure that buffers are in garbage collection speed for occasionally not
flushed, the file is closed and resources freeing some actual garbage.
associated with the file are returned to the
operating system. Two basic approaches in distinguishing
live objects from garbage are reference Counting
and tracing. Reference counting garbage
Another use is where a program wants collectors distinguish live objects from garbage
to keep a list of objects that are referenced objects by keeping a count for each object on the
elsewhere. The program may want to know what heap. The count keeps track of the number of
objects are in existence for, say accounting references to that object. Tracing garbage
purposes but does not want the mechanism of collectors actually trace out the graph of
accounting to prevent objects from otherwise references starting with the root nodes. Objects
being freed. Garbage Collection Algorithms that are encountered during the trace are marked
in some way. After the trace is complete,
In our collector the programmer can unmarked objects are known to be unreachable
specify the function (finalizer) that he wishes be and can be garbage collected
called when the object is to be freed to perform
clean up activities.
Garbage detection is ordinarily a) Whenever the number of bytes that have been
accomplished by defining a set of roots and allocated by our allocation routine exceeds a
determining reachability from the roots. An predefined limit. That is why we call our
object is said to be reachable if there is some collector to be periodic and the programmer can
path of reference from the roots by which the set this period if he wishes to. By default this
executing program can access the object. The value has been set to 640K bytes of memory.
roots are always accessible to the program. Any
object that is reachable from the roots is b) Whenever the allocation routine is unable to
considered to be live. Objects that are not allocate the memory as such. That is if malloc
reachable are considered garbage as they can no returns NULL. In this case we call our collector
longer affect the future course of program to collect the garbage and then try to re-satisfy
execution. the request for memory.
Our GC Library provides the following garbage collection and during the freeing of this
routines and they act as the programmer’s memory location the function gc_finalizer will
interface to the collector. get executed
Output: Starting address of the memory location Description: This function is responsible for
that has just been allocated if successful and freeing the memory location gc_waste
NULL if allocation fails. and add them to the free list. The memory
allocated using the function
Description: This function allocates a memory GC_malloc_uncollectable can be freed by using
location of size gc_size and returns the address this function.
of that memory location if successful in
allocation. If the requested memory cannot be f) void GC_set_gc_rate ( size_t max_heap_size)
allocated even after Garbage collection then
this routine returns NULL. The specialty of this Input: It takes the memory size as argument.
function is the memory allocated by
GC_malloc_uncollectable will not be freed Output: None
during the garbage collection
Description: This function is responsible for
c) void * GC_malloc_finalizer (size_t gc_size , setting the threshold value for allocated memory.
void ( * gc_finalizer)() ) If the memory allocated dynamically by the user
exceeds the threshold, the garbage collection
Input: gc_size, gc_finalizer routine will be called automatically.
Distinguishing the live objects from the 1) The user requests for allocation using the
garbage or garbage detection. function “GC_malloc” or any of its Variants.
Reclaiming the garbage objects storage 2) We try to satisfy his request by allocation an
so that the running program can use it object of the size requested by the user from the
or Garbage collection. heap.
4) If memory request is satisfied and memory so ii. The book keeping list gives us the
far allocated > GC Limit information about what to search?
Begin And the root pointers will give us the
Call the GC routine; information about where to search?.
Do book-keeping activity;
Return the address that corresponds to iii. Having got the information about
the allocated memory region; what to search? and where to search?,
End our algorithm gets in to the mark phase
first for finding and marking the
5) If memory request is not satisfied garbage and then in to the sweep phase
Begin for collecting the garbage.
Call the GC routine;
Try to reallocate; Mark Phase
If (allocation satisfied)
Return address of the allocated If an object is found to be alive then
object mark it. This is done by setting a field in the
Else book-keeping node corresponding to this object.
Return ERROR;
End if
End
Sweep Phase