0% found this document useful (0 votes)
73 views

PL101 - Programming Languages

This document discusses memory management in programming languages. It explains that memory is divided into text, stack, heap and global data sections. Manual memory management in C can cause dangling pointer and space leak problems. A dangling pointer occurs when a pointer refers to freed memory. A space leak happens when allocated memory is not freed. Garbage collection helps prevent these issues but has limitations like memory still leaking in cyclic references and non-memory resources not being cleaned up. The document assigns students to identify memory management issues in a given programming language and provide a sample program demonstrating a problem like a dangling pointer or space leak.

Uploaded by

Divine Manalo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

PL101 - Programming Languages

This document discusses memory management in programming languages. It explains that memory is divided into text, stack, heap and global data sections. Manual memory management in C can cause dangling pointer and space leak problems. A dangling pointer occurs when a pointer refers to freed memory. A space leak happens when allocated memory is not freed. Garbage collection helps prevent these issues but has limitations like memory still leaking in cyclic references and non-memory resources not being cleaned up. The document assigns students to identify memory management issues in a given programming language and provide a sample program demonstrating a problem like a dangling pointer or space leak.

Uploaded by

Divine Manalo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

PL101 –

Programming
Languages
Objectives

At the end of the presentation the students must be able to

• Explain how programming language implementations typically organize memory i


nto global data, text, heap, and stack sections and how features such as recursion a
nd memory management map to this memory model

• Identify and fix memory leaks and dangling-pointer dereferences.


• Discuss the benefits and limitations of garbage collection, including
the notion of reachability
Memory Management

Memory is used to store the program itself and the run-time system nee
ded to support it.
Manual memory management

C Language

A significant problem with manual memory management is that it is possibl


e to attempt to use a pointer after it has been freed. (dangling pointer proble
m)

Another potential problem of manual memory management is not remembe


ring to free allocated memory when it should be freed. (space leak)
What are the memory segments?

Segmentation is the process in which the main memory of the computer is logically d
ivided into different segments and each segment has its own base address. It is basicall
y used to enhance the speed of execution of the computer system, so that the processor
is able to fetch and execute the data from the memory easily and fast.

• Allows the memory capacity to be 1 Mbyte although the actual address to be


handled are of 16-bit size.

• Allows the placing of code data and stack portions of the same program in different
parts i.e segments of the memory, for data and code protection.

• Permits a program and its data to be put into different areas of memory each time
program is executed, i.e, provision for relocation may be done.
What are the memory segments?
•text (code) segment
The text segment (often called code segment) is where the compiled
code of the program itself resides. 

•stack segment
The stack is the section of memory that is allocated for automatic
variables within functions.
LIFO

•heap segment
An area of memory used for dynamic memory allocation.
What is dangling pointer?

Dangling Pointer

A pointer pointing to a memory location that has been deleted (or freed) is called dang
ling pointer. There are three different ways where Pointer acts as dangling pointer.

1.  Deallocation of Memory


// Deallocating a memory pointed by ptr causes dangling pointer

2.  Function Call


    // The pointer pointing to local variable becomes
    // dangling when local variable is static

3. Variable goes out of Scope

    
What is dangling pointer?

Deallocation of Memory

// Deallocating a memory pointed by ptr causes dangling pointer


 
#include<stdlib.h>
#include<stdio.h>
     int main()
    {
          int *ptr = (int *)malloc(sizeof(int));
    
          // After below free call, ptr becomes a dangling pointer
          free(ptr);
          // No more dangling pointer
          ptr = NULL;
     }
What is dangling pointer?
• Function Call

// The pointer pointing to local variable be   // Driver code


comes dangling when local variable    int main()
is static    {
    int *p = fun();
    fflush(stdin);
#include<stdio.h>     // p points to something which is not
   int *fun()     // valid any more
    {      printf("%d", *p);
     // x is local variable and goes out of sco     return 0;
pe    }
     // after an execution of fun() is over.
     int x = 5;
    
     return &x;
    }
•     
 
What is dangling pointer?

Variable goes out of Scope

void main()
{
   int *ptr;
   .....
   .....
   {
       int ch;
       ptr = &ch;
   }
   .....   
   // Here ptr is dangling pointer
}
What is a space leak?
Space Leak

There is a space leak when memory is lost and cannot be reclaimed. Space cannot be lo
st for-ever without reaching the limit on the available memory. A long-running progra
m with a space leak will eventually crash.

A space leak occurs when a computer program uses more memory than necessary

Detecting space leaking fragments of code in a large program is extremely challenging.


A program with a space leak will often reach its peak memory use in the middle of the
execution.
What is a space leak?

Space Leak

Space leaks are often detected relatively late in the development process, an
d often only in response to user complaints of high memory usage.
If space leaks could be detected earlier say for example as soon as they were i
ntroduced then they would be easier to fix and would never reach end users

Space leaks remain a thorn in the side of lazy evaluation, producing a signifi
cant disadvantage. (Haskell)
What is Memory Leaks?

Memory Leak

A memory leak occurs when memory is allocated for something but is no


longer needed and now occupying RAM needlessly. 

It also occurs when programmers create a memory in heap and forget to


delete it.

If a memory leak is occurring consistently and a program is using more mem


ory that is required, then it will eventually take up all available memory.

Performance will be continuously slowed down until the application crashes


or until the program is closed completely.
Memory Leaks
A memory leak is any portion of an application which uses memory without
eventually freeing it. 

The reality is that memory leaks can strike any application in any language.
They’re more common in older or “closer to the metal” languages like C or C
++.

#include <stdlib.h>
#include <unistd.h>

int main() {
while(1) {
malloc(1024); // memory leak!
sleep(0.5); // slow down the leak
}
return 0;
}
How can I detect a memory leak?

System slowing down

The simplest way to detect a memory leak is also the way you’re most likely to find on
e: running out of memory. 

If you do, it can be time to start digging into your code to figure out just what’s using u
p all your RAM.

Profiling tool
A profiling tool isn’t a silver bullet: it won’t tell you right away which parts of you
r application are leaking memory. What it will do is tell you which parts of your applica
tion are using the most memory. 

Another method for memory leak detection is to use logging intelligently.


How can I prevent a memory leak?

Garbage collectors

It frees up memory that the application doesn’t need.

Garbage collectors are fairly complicated bits of code.

Another important way to prevent memory leaks is to write code which disposes of un
needed resources.

If you’ve found a memory leak in your code, don’t view it as a failure of software. Inste
ad, look at it as a chance to improve your craft

Java, Ruby, PHP, Python are languages with garbage collectors


Benefits of Garbage Collection

•Frees developers from having to manually release memory.

•Allocates objects on the managed heap efficiently.

•Reclaims objects that are no longer being used, clears their memory, and keeps the m
emory available for future allocations. Managed objects automatically get clean conten
t to start with, so their constructors don't have to initialize every data field.

•Provides memory safety by making sure that an object cannot use the content of anot
her object.
Limitations of Garbage Collectors
Memory can still leak
In automatic reference counting systems, such as Perl or Objective-C, memory is
leaked whenever there are cyclical references, since the reference count is never
decremented to zero.

A stranger type of memory leak can occur if the garbage collector is conservative. A
conservative garbage collector assumes that any memory that looks like a valid
address to an allocated object is a pointer to that object.

Not everything is memory


Garbage collection is designed to automatically clean up unused memory resources.
However, garbage collectors generally don’t handle cleanup of non-memory resources
very well.
Limitations of Garbage Collectors

The cost of garbage collection


Garbage collection isn’t free. Regardless of the strategy collecting the garbage takes
CPU cycles that could be used for other work. 
Assignment

Identify the memory management and the problems encountered with the
assigned programming language.

Show your sample program next meeting(for example is there a dangling


Pointer or space leak?).
Thank you!

Keep Safe Everyone

You might also like