Why memory allocated through malloc should be freed ?

Thread Tools Search this Thread
Top Forums Programming Why memory allocated through malloc should be freed ?
# 15  
Old 08-31-2009
Originally Posted by karthiktceit
No the output does not change at all.
But many senior professionals in my office say that memleak is a serious issue.
Sure, it can be. Is this statement general or based to issues you have with your application ?
If you dont free it, it will cause fatal problems during execution.
It will eventually.
Can anyone tell me what memory leak would do to the process that caused it and other process running parallely?
A memory leak will first slow down the system when no more RAM is available and the OS start paginating (swapping pages) intensively. After some time, the system might starve virtual memory too. What will happen will depend on what OS and kernel you use. Linux is known for its controversial OOM killer, a kernel task that blindly kill processes in order to free some memory.

Last edited by jlliagre; 08-31-2009 at 01:30 PM..
# 16  
Old 08-31-2009
memory leak problem should not be handled carelessly and should not be left to OS to handle that. It should be handled with care.

On your desktop or test host only ( not in prod ), write a program that only does malloc() without free() for every 30 seconds and see the system performance over period of time. Difference will be quite obvious.

Also, think of a ever running program like daemons and a memory leak problem in it, over a period of time, this memory leak problem will manifest itself and disturb the system.
# 17  
Old 08-31-2009
I understand that, But any part of the process's address space being leaked leads to a memory leak. But stack, data and text segments' memory is not at all leaked. Beacuse they are freed by the OS itself when a process terminates. But Heap alone needs to be freed explicitly. why is that so?
No. Completely no. Heap memory is released when the process ends, whether or not the program called free(). The memory is released. After a process ends, the old process heap memory is no longer allocated to any process. Read man page for brk().

For long running programs and BEST PRACTICE in C coding, always call free().

I put this here as a correction. Not a comment.
# 18  
Old 09-01-2009
Then I undertand that memory leak is meaningful as long as the process that caused it is alive. Once the process that caused it dies then all the leaked memory will made available to the OS memory manager. Am I right?
# 19  
Old 09-01-2009
Correct. All memory - even leaked memory - that belongs to a process is reclaimed on process termination.
# 20  
Old 09-04-2009
Considering the lifetime of a process, assuming that its short lived and its not ok to accept memory leaks in the code.

1. Same thing might happen to a daemon - which will bring down system
2. Trigger OOM errror
3. A bug in short lived process (assuming its short lived) which eventually might be using up all the resources.
4. Even for a short lived process, if the memory consumption is exponential, then we are done in much lesser time

So, the rule is - no memory leaks in the code and no memory leaks whatever be the life span of the process memory image Smilie
# 21  
Old 09-08-2009
Originally Posted by karthiktceit
You mean to say that there wont be any problem at any point of time if there is memory leak in my code. right?
Except at runtime because the process can consume all 4Gb (32-bit process) such that it ultimately freezes and cannot continue. On machines with little RAM this is a serious problem as it can prevent other processes from starting or allocating memory. When the leaking process terminates all memory allocated from text data and stack segments is released.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change allocated memory for a process?

Hello, I am running ubuntu 14.04 in a server with 32GB ram. Due to receiving "high load" errors during ssh connection, I took a look at what's happening from command line. I detected that 20GB of total memory was allocated to a program. Below you can see some initial part of installation... (4 Replies)
Discussion started by: baris35
4 Replies

2. Red Hat

KVM/Qemu allocated memory not showing in guest

So we have a RHEL 7.6 workstation with 128 gigs of ram. The OS sees all the ram and 80 cors (40 HT) We have 1 guest with 8 CPUs and 32gigs of ram running RHEL 7.6 workstation as well. We are trying to create another guest with 64 CPUs and 80 gigs of ram. We setup the system using... (0 Replies)
Discussion started by: joeg1484
0 Replies

3. Programming

glib detected: malloc() memory curruption

I am using libxml2 library for XMl parsing and libxml++ is C++ wrapper over that. So I am using API of libxml++. I am creating my class and composing instance xmlpp::Node *pNode in that. my class also have funciton prepareXPathQuery() which creates query string and have other fucntion... (2 Replies)
Discussion started by: sharadwagh
2 Replies

4. Programming

./match_pattern.out: malloc(): memory corruption: 0x0000000013a11600 ***

Hi All, I have a simple code which does some computation by matching string patterns. In brief: 1. The code reads .dat and .txt files. 2. .dat files are huge text files and .txt files contain some important words. 3. I am just doing strstr to find the patterns. 4. The function returns the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

5. Programming

Regarding the maximum memory allocated by malloc() function on HP-UX B11.11

In a 'C' program,when I am trying to allocate memory with the help of malloc () function, it is allocating the memory up to a certain limit for e.g. in my case, it is 670 MB (approx). malloc() returns NULL if I allocate more than this amount of memory.When I tried to allocate memory in chunks of... (1 Reply)
Discussion started by: vipinsachan
1 Replies

6. HP-UX

how to find size of memory allocated to a pointer?

Hi, Am new to HP UX , is there a way to find out the size of memory allocated to a pointer on hp ux? For example we can use the _msize() on windows to find the size of memory allocated to a pointer . #include <stdio.h> #include <malloc.h> void main() { void *buffer; ... (0 Replies)
Discussion started by: Wkdunreal
0 Replies

7. AIX

amount of memory allocated to large page

We just set up a system to use large pages. I want to know if there is a command to see how much of the memory is being used for large pages. For example if we have a system with 8GB of RAm assigned and it has been set to use 4GB for large pages is there a command to show that 4GB of the *GB is... (1 Reply)
Discussion started by: daveisme
1 Replies

8. Solaris

Memory usage in Solaris - memory not freed?

Hi, I'm running a multi-process software system on a Solaris 8 machine. When I monitor the memory usage, I see that the free memory is dropping rapidly, but I can't detect a process that uses this memory. I'm using "top" to get the free memory and the memory usage of processes. Thanks. (3 Replies)
Discussion started by: gewurtz
3 Replies

9. Programming

malloc gives the same memory to two different nodes. How to deal with it?

When allocating memory for two different nodes, the resulting memory are the same. Clearly, this will lead to a mistake. This happened in a function. And the process must be in a function. (gdb) p tree->list $43 = (node *) 0x8be4180 (gdb) p tree->list $44 = (node *) 0x8be4180 At the... (2 Replies)
Discussion started by: cdbug
2 Replies

10. Solaris

Memory allocated

Hi, How to find out what is the maximum memory allocated to TOMCAT server in SunOS 5.8? The Tomcat server crashes down during peak times.... Regards (1 Reply)
Discussion started by: baanprog
1 Replies
Login or Register to Ask a Question