Memory ManagementWhat is memory management?TerminologyWhat’s a good MM scheme?Fixed v. varying size blocksLinked v. unlinkedIf unlinked, thenSmall v. largeTime v. memoryExplicit v. implicit releaseScheduled v. unscheduled releaseInitialized v. uninitialized blocksRecords of a single sizeSlide 14Reference countsSlide 16Reference CountsSlide 18What else can we do?Memory ManagementCSCI 2720Spring 2005What is memory management?“the prudent utilization of this scarce resource (memory), whether by conservation, recycling, or other strategies.”Terminologyheap** - a table M[0..N-1] of N cellsSmaller blocks of memory are allocated from this heapAn allocated block is said to be reserved or in useThe allocated block may later be freed or deallocated.** -- no relation to the partially ordered, implicitly represented tree we just talked about in class.What’s a good MM scheme?It depends:Blocks of fixed size v. varying sizeLinked blocks v. unlinked blocksSmall blocks v. large blocksTime v. memoryExplicit v. implicit releaseScheduled v. unscheduled releaseInitialized v. uninitialized blocksFixed v. varying size blocksIf fixed, or large number of same size, makes sense to set aside a heap for those requestsFixed-size easier to manageLinked v. unlinkedIf linked then can’t arbitrarily move blocks:Example:Can’t relocate B without breaking AA BIf unlinked, thenProgram AProgram BProgram CProgram AProgram BProgram CProgram DSmall v. largeMay need to handle a few bytes … or megabytesSmall blocks –May make sense to move, zero out, etc.Large blocksWant to avoid operations whose performance depends on size of blockTime v. memoryWhich are you optimizing?If you can “waste” some memory (have a large heap), then may be able to use faster management algorithms.If memory is scarce, may need more time-consuming algorithms.Explicit v. implicit releaseDoes “user” notify memory manager when memory is no longer needed by program?Garbage = memory that MM has allocated, but that is no longer referenced by any variable that can/will be accessed by client.When can MM reclaim memory?Scheduled v. unscheduled releaseMemory manager can benefit from knowing order in which memory will be released.(For example: if release is LIFO, can use a stack.)Initialized v. uninitialized blocksInitialize to all zeros to meet semantics of programming language.Initialize for security purposes.… but takes time proportional to size of block.Records of a single sizeGiven heap of memory M[0..N-1], if records are all of same size k, can partition at init into a table of cells of size k, T[0..m-1], where m = floor(N/k)For any allocation request, return some T[i]At any time, some cells are in use, some are free; no particular order or pattern to their statusRecords of a single sizeMaintain a free list: singly linked list of cells not in use. No additional memory needed, can use part of cell as pointer to next chunk. Assumes size(cell) > size(pointer)Access as a stack: Pop to allocate, Push on deallocation, thus (1).Garbage collection problem remains: when is a chunk available to be reclaimed?Reference countsSuppose we have:47xyyReference countsEach time the address of a cell is copied, we have another “use” of that memory locationCan’t deallocate until all copies of the address have been destroyed.Method for keeping track of the use of cells: maintain a reference count for each cellReference Countsxy47Reference CountsCons:Lots of machinery:P <- Q requiresDecrement ref count of *PIf *P is now 0, deallocateIncrement *Q Requires memory in cells for ref countAnd how big should it be??Circularity a problemWhat else can we do?Mark and sweep garbage
View Full Document