answersLogoWhite

0

You are probably referring to a resource leak, however a resource leak specifically relates to programs that allocate memory on the heap (the free store) but fail to keep track of it. If we lose track of a heap allocation, the programmer cannot release that memory back to the system until the program terminates. The following is an example of a resource leak:

void f (unsigned x) {

void* p = malloc (x); // allocate x bytes // ... use memory

}

Each time we call this function we allocate another s bytes on the heap. But the function never releases that memory and because p is local to the function, we have no way of knowing where that memory was allocated (p will fall from scope when we return from the function). If we call this function often enough, we will eventually run out of memory.

To fix the problem, we must release the memory as soon as we are finished with it:

void f (unsigned x) {

void* p = malloc (x); // allocate x bytes // ... use memory

free (p); // release the memory

p = NULL; // zero the pointer

}

It is not necessary to zero a pointer when the pointer will fall from scope anyway, but it is considered good programming practice. What is important is that we release the memory it refers to before it falls from scope. Keeping our scopes as narrow as possible helps us to manage our resources more easily; the larger the scope, the more easily we can inadvertently introduce resource leaks, particularly when our code is maintained by several different programmers.

The problem you describe isn't necessarily a resource leak but it is clearly wasteful to allocate memory that is left unused even if we don't create a resource leak. Good programming practice dictates that we release memory as soon as we no longer require it, however allocating memory on an as-required basis can impact upon performance so it is not unusual for a programmer to allocate more memory than he needs, thus creating a reserve that can be used as and when required. Not using a reserve isn't in itself a problem, however we should endeavour to keep our reserves to a minimum and return all reserved memory to the system in a timely manner -- as soon as we no longer require it.

User Avatar

Wiki User

7y ago

What else can I help you with?

Continue Learning about Engineering

How the new keyword allocates memory?

The new keyword in programming languages like C++ and Java allocates memory on the heap for an object or data structure. When new is called, it requests a block of memory sufficient to hold the specified type, initializes that memory (if applicable), and returns a pointer or reference to the newly allocated memory. This memory remains allocated until it is explicitly deallocated using delete in C++ or is automatically reclaimed by the garbage collector in languages like Java. Proper memory management is crucial to avoid memory leaks and ensure efficient use of resources.


When you compile source code where the variables stored?

When you compile source code, the variables are stored in memory during program execution. The compiler translates the source code into machine code, which allocates memory for variables in different segments, such as the stack (for local variables) and the heap (for dynamically allocated memory). The specific location and management of these variables depend on the programming language, the compiler, and the architecture of the system. Additionally, constants and global variables may be stored in separate memory regions.


What is phantom memory in java?

Phantom memory is a false memory which doesnt exist in reality .These references are available to collect byGC


What is malloc vs calloc?

malloc allocate a memory section whereas memset manipulate the content of the memory section, (for example fill a memory section pointed by pointer ptr with 0, we use memset(ptr,0,sizeof(ptr_data_type)) A memory section must be allocated(using either 'malloc' or 'new' in C++) before memset can be used on it.


What is profiling in IT industry?

Profiling refers to the act of running a program (called a "profiler") which analyzes a program you've written. The profiler will collect information about your program, such as: what functions are called, how often each is called, how much time is spent in each function, how much memory is used, etc.. This information can be used to help improve the performance of your program.

Related Questions

Difference between contiguous and non contiguous memory?

In contiguous allocation there is no overhead during execution of a program. In noncontiguous allocation address translation is performed during execution Contiguous memory allocates single area of memory Noncontigious memory allocates several memory areas - one memory are to each component of a process


What is name for memory is a concept in which the operating system allocates a portion of a storage medium to function as additional RAM?

It is commonly called "virtual memory".


What is the difference between contiguous and noncontiguous text?

Dfference between contiguous and noncontiguous1) In contiguous allocation there is no overhead during execution of a program.1)In noncontiguous allocation address translation is performed during execution2)Contiguous memory allocates single area of memory2)Noncontigious memory allocates several memory areas - one memory are to each component of a process


Is RAM called as program memory?

no , it is called random access memory


Who allocates the memory?

Allocation is performed by OS while memory detection is done by BIOS.


What is a memory footprint?

A memory footprint refers to the amount of memory that a program or application uses while it is running. It includes the memory required for the program's code, data, and any resources it allocates during execution. Understanding memory footprints is crucial for optimizing performance and ensuring efficient resource management, especially in environments with limited memory. Monitoring memory footprints can help developers identify memory leaks or inefficiencies in their applications.


What program in an OS coordinates the memories by tracking which one is available which is to be allocated or deallocated and how to swap between the main memory and secondary memories?

The memory manager is the program responsible for managing memory allocation and deallocation in an operating system. It keeps track of which memory is available and allocates it to processes based on their needs. It also handles swapping data between main memory and secondary storage when the system runs low on physical memory.


What is kernel and shell in unix?

The kernel The kernel is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls. The shell The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell.


How the new keyword allocates memory?

The new keyword in programming languages like C++ and Java allocates memory on the heap for an object or data structure. When new is called, it requests a block of memory sufficient to hold the specified type, initializes that memory (if applicable), and returns a pointer or reference to the newly allocated memory. This memory remains allocated until it is explicitly deallocated using delete in C++ or is automatically reclaimed by the garbage collector in languages like Java. Proper memory management is crucial to avoid memory leaks and ensure efficient use of resources.


What actually happens when function get called how the compiler is reading the next instruction after completing that function?

When a function gets called, the processor first stores the address of the calling function in a structure called activisation structure and jumps to the called function. Then it allocates the memory for the local variables and initializes them. Then it does the processing in that function. After that it deallocates the memory allocated for the local variables and returns to the calling function. When a function gets called, the processor first stores the address of the calling function in a structure called activisation structure and jumps to the called function. Then it allocates the memory for the local variables and initializes them. Then it does the processing in that function. After that it deallocates the memory allocated for the local variables and returns to the calling function.


Roles of kernel in an operating system?

part of OS that allocates memory,CPU and other resources


How memory allocates for node in data structures?

I guess it's functions malloc and free, what are you thinking of.