ERODD HOME

Operating Systems CPSC 304
Earl Rodd erodd@malone.edu Extension 8546 www.malone.edu/erodd
Memory Management Lab (memory leak)

Revised 9/15/2008

Name: ______________________________

Overview

In this lab, you will work with a "C" program in both LINUX and in Windows and a Java program in LINUX. In addition, you will learn to use the LINUX "top" command to monitor resources. You will also use the Windows Task Manager to monitor resources.

For convenience, this lab is organized to do the Windows portion first, then do all of the LINUX parts. You may of course do the LINUX parts first.

C Program with Memory Leak

In this lab, you will be working with a "C" program which uses the "C" library calls malloc() and calloc() to request memory from the operating system. The memory is allocated in the user address space and is read/write to the program.

malloc() and calloc() perform similar functions.

The source of the LINUX version of the program is shown below:

// Earl Rodd - Malone College // Takes one parameter which is interation after which to stop and // prompt to press enter each iteration. // Uses malloc // This program loops obtaining memory. // When memory is not available, then the program exits. // An initial malloc() is used. This does not initialize memory. // Then calloc() is used which clears memory. Experience shows that // if memory is allocated, but not used in any way, then it may not // really take space! // Just to be sure, a character is put at the end of the // allocated area. // Technical note: This program represents a fatal memory "leak" // in that memory is continually allocated, but never // freed. // Think of a program which did what this program does, // except that the allocates are spread out over time // (minutes, hours, or days). // If the program with the "leak" happened to be in the // kernel, then the operating system itself fails. // // #include <stdio.h> #include <stdlib.h> #include <math.h> #include <sys/types.h> #include <errno.h> #include <curses.h> // UNIX only // #include <cursesw.h> // UNIX only // #include <slcurses.h> // UNIX only int main(int argc, char *argv[]) { char *e1[2500]; // Array of pointers to the memory blocks allocated char *m1; int i,diffnum; int id,aid; int stopat; // What interation to stop and ask to continue int msize, asize, size1; double totmem; // Must be a double to hold a big enough number printf("Starting mtest\n"); if (argc < 2) { stopat = 99999; // I.e. never stop and prompt } else { stopat = atoi(argv[1]); } msize = 524288; asize = 8; size1 = 4194304; m1 = (char*) malloc(size1); // Initial 4 meg demonstrating malloc // m1 is pointer to memory totmem = size1; for (id = 0; id < 2500; id++) { e1[id] = (char*) calloc(msize,asize); // Gets asize chunks of msize // bytes. if (e1[id] != NULL) // Be sure got some memory { totmem = totmem + msize * asize; // Current total printf("Loop index: %d Got 4Meg memory at address %p Total: %.0f\n", id,e1[id],totmem); m1 = e1[id] + msize -2; // point to end of memory block *m1 = 'B'; // Put something there at end } else { printf("ERROR for request %d\n",id); printf("Program exiting!\n"); break; } if (id > stopat) { printf("Press Enter to continue...\n"); i = getchar(); } } }

The Windows version differs only in the include file(s) needed for the "getchar()" or "getch()" function.

What the Program Does

Normally, when a program has a memory leak, the memory "leaks" over time. For example, perhaps every time you open a new window or pull down, the program allocates memory, but fails to ever free the memory. Thus the memory "leaks" to the program away from available memory.

This program "leaks" really fast! It runs in a loop performing allocations of 4 Meg of memory. It runs until either 10 Gig is allocated (more than supported on Pentiums) or until a memory allocation fails. It displays the current total memory allocated on the screen.

This program was compiled and linked in both LINUX and in Windows.

The program name is:

LINUX: /home/square/tdrive/erodd/bin/aleaklin Note: First do the "mnet logon student userid" command (to get the network drives). Use your normal network userid/password. Note: This directory is in the PATH on the lab systems, so once you have done the "mnet logon", you can just type aleaklin at the command line. WINDOWS: R:\ERODD\BIN\MLEAKWIN.EXE Or: R: cd \erodd\bin mleakwin

The program takes an optional command line parameter. If provided, the parameter is the iteration of getting 4 Meg of memory after which to stop and wait for the user to press ENTER prior to each subsequent get of 4 Meg of memory. This allows the user to leave the program running with a lot of memory allocated in order to observe memory usage using system monitors.

WINDOWS Lab

  1. Open a command window (Start/Run/CMD.EXE).
  2. Run the MLEAKWIN program.
  3. Describe how fast the program runs?

    __________________________________

  4. How much memory is allocated before an allocate fails?

    _____________________________________

  5. Run the program multiple times. Does the amount allocated before failing change from run to run?

    _________________________________________________

    _________________________________________________

  6. Describe why the operating system does not crash when this program is run.

    ______________________________________________________________

    ______________________________________________________________

    ______________________________________________________________

    ______________________________________________________________

  7. Notice the iteration on which the program fails. Now run the program specifying an iteration 2 or more iterations before the failure so that the program will stop at that point. For example, if the program fails at loop index 500, the run:
    R:\erodd\bin\mleakwin 496
    

    With the program running, start the Windows Task Manager by right clicking somewhere in an empty area of the task bar. Look at Swap File usage (in the Performance tab in a box labeled PF Usage - PF stands for Page File). What does it show about Swap File (PF=Page File) usage?

    ________________________________________________________

  8. Leave the copy of mleakwin running.

  9. Open a second command line window. Run mleakwin
       R:\erodd\bin\mleakwin
    

    What happens? ________________________________________________

  10. In the second window, run with a parameter about 20 less than the failure point (similar to what we did in step 7, but it might be different this time). Leave the program running.

    Check the swap file usage. What changed?

    ________________________________________________

  11. Leave the two copies of mleakwin running in the two windows. Open a third command line window. Run mleakwin.
       R:\erodd\bin\mleakwin
    

    What is the swap file (PF) usage? _________________________________________________

    Explain the results. _______________________________________________

    ____________________________________________________________________

  12. In this third window, run mleakwin, and with a parameter several less than the count on which it terminated.

    What swap file usage is shown now? ________________________________________

    ______________________________________________________________________

  13. Terminate all 3 copies of mleakwin (either CTRL-c or just close the command windows).

    How what is the swap file (PF) usage? _______________________________

LINUX Lab

  1. First do "mnet logon student userid" where userid is your Malone userid. This gives you access to the tdrive.

    Then do this command in LINUX:

    newprof

    and close that terminal window. Open a new terminal window to start the lab. The newprof command sets the CLASSPATH correctly for the JAVA portion of the lab. This needs done only once ever for a given machine.

  2. Examine the difference in the malloc() and calloc() system functions. Do the command(s) man malloc man calloc Note: Both may be described in the same man page.

    Describe the difference in the two functions.

    ________________________________________________________

    ________________________________________________________

    ________________________________________________________

  3. Now run the program (aleaklin).
  4. Describe how fast the program runs?

    __________________________________

  5. How much memory is allocated in LINUX before an allocate fails?

    _____________________________________

  6. Run the program multiple times. Does the amount allocated before failing change from run to run?

    _________________________________________________

    _________________________________________________

    __________________________________________________________

  7. Start the "top" monitor in another LINUX command line window. Customize "top" with these commands: u square (ENTER) # show only processes for your userid z # change to color mode so can see highlights x # may highlight which column is sorted on >>> # should sort on the "command" column Note: Column being sorted on is a different color R # Reverse sort so aleaklin will show first
  8. Now, notice the iteration on which the program fails. Now run the program specifying an iteration 2 or more iterations before the failure so that the program will stop at that point. For example, if the program fails at loop index 500, the run:
    aleaklin 496
    

    In "top", for the "aleaklin" program, what values are shown for %MEM, VIRT (Virtual memory usage - this shows Kilobytes unless another unit is explicitly shown), total SWAP file size, and Swap file usage?

    %MEM ___________________________________________________

    VIRT ___________________________________________________

    Swap File Size__________________________________________

    Swap File Used _________________________________________

    Note: In the lab, there is no SWAP file on the LINUX systems.

  9. End the program either be pressing ENTER till it ends or by using CTRL-c.
  10. Try another version of the program:
    /home/square/tdrive/erodd/aleaknew
    
    This version of the program not only allocates the memory and writes one byte in each block, but writes random data in every single byte. This will force LINUX to actually write all the pages to the SWAP file instead of being clever noticing that the pages have never changed.

    Run this version of the program (which runs slowly). Be patient! and let it finish. Don't mess with the machine while it is running (e.g. using the browser uses a lot of memory and will cause problems).

    For this program (aleaklin), in top, how large does the VIRT value get? _______________________ How large does the %MEM value get?

    What finally happens?

    _______________________________________________________

  11. When you see at which step "aleaknew" fails, run it again with a parameter of 25 less than the failing number. So if it fails on step 100, run with "aleaknew 75".

    For this program, what are the values in top for VIRT and %MEM? _________________ _______________

  12. Now, with the prior step still running, open another terminal window and run aleaknew again with no parameter? (Be Patient!)

    What happens? ___________________________________________________

    Explain what happened? _________________________________________

  13. End the "top" command by typing "q".

Memory Leak with Java (do this in LINUX)

In this section of the lab, we will use a Java program which has a significant "memory leak". The program just keeps allocating space for large arrays. The program has a timer delay so that it runs slow enough to see the resource usage in the "top" monitor. The timer delay is adjustable via a command line parameter. You may adjust the speed with the goal of a) running slow enough to see the memory usage in the monitor and b) running fast enough so that you are not waiting for several minutes.

If you would like to see the Java code, it is in the file: /home/square/tdrive/erodd/mleakj.java

Mechanics

  1. If you did not run the newprof command on this machine, run it now and close that terminal window.
  2. If you have left "top" running from the prior step, stop it and start it again entering these commands after starting top: u square (ENTER) # Show only for the one userid x # Highlights the sort field z # Color >>> # Press multiple times until the COMMAND column # is highlighted.
  3. Start the program in a new terminal window (leave "top" running): java mleakj

    Note: The default "speed" is 3. To run slower:

    java mleakj 2 or java mleakj 1 To make the program go faster: java mleakj 5 # any positive number is valid. The higher, the faster.
  4. What happens to the program?

    _________________________________________________

  5. Look at "top" columns: VIRT and %MEM (Look for command java).

    How high did they go?

    VIRT _______________ %MEM _____________________

  6. Now run the Java virtual machine with a large memory allocation pool. The default is 256Meg. Try first with 500M with the command:
      java -Xmx500m mleakj 5  # Choose speed to suit your purposes
    

    Note: There cannot be any blanks in the "-Xmx500m" parameter.
  7. What happens to the program?

    _________________________________________________

  8. Look at "top" columns: VIRT and %MEM
  9. How high did they go?

    VIRT _______________ %MEM _____________________

  10. Try again with 800 Meg allocated to Java.
      java -Xmx800m mleakj 5  # Choose speed to suit your purposes
    
    Be patient!
  11. What happens to the program?

    _________________________________________________

  12. Look at "top" columns: VIRT and %MEM
  13. How high did they go?

    VIRT _______________ %MEM _____________________