Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have a program that all it does is to allocate memory up until consume 1GB of free resources. but when i delete it, it seemed that the space is not free to kernel, (notice this by looking at "top" or meminfo, as well as debug messages prinf the memory info. using sysinfo (system call). this happens on mainline kernel 2.6.13 but not on other Redhat distros (RHEL3/RHEL4).
so it seemed that on mainline 2.6.13, when the userprocess allocate mem and free it mem, the freed memory is not returned back to the kernel... is this a possible bug????
How does it allocate all this space? Are you making multiple calls to 'malloc()' or similar? If you close the program without making the subsequent calls to free() (for the total amount of memory malloc'd) then the memory won't be freed back for other programs to use and so will remain in use.
Please Observe this test program:
Assumption: in main() the space (units in KB) to allocate is 1GB, if you machine has less than that use lower space value- 100MB (to be left to avoid oom killer).
Idea: allocate using a linked list to as many nodes as it required to filled up 1GB or less of address space
Result: on RHEL3 or 4, after the program allocate nodes, and then deallocate it, sysinfo indicate the memory that were freed are returned to the kernel.
on 2.6.13, after the proram allocate nodes, and then deallocate it, sysinfo give the free{ram+swap} to be about the same as it was after the node finish
allocating, seemed like the freed nodes address space were not returned to the kernel
//////////////////////// CONST DATA ///////////////////////////////
#define LIMIT 102400 /* = 100MB (Units in KB) */
#define KB_VALUE 1024 /* Unit in bytes */
#define ARR_SIZE 1024 /* array size for item data in node */
///////////////////////////////////////////////////////////////////
/********************************************************************
* Node Data Structure for LLL
* size of this struct ~ 64 Bytes
********************************************************************/
struct node
{
int item[ARR_SIZE];
struct node * next;
};
////////////////////////////// GLOBAL DATA ////////////////////////////
struct node * head = NULL; /* head of LLL */
long unsigned count_create = 0;
long unsigned count_destroy= 0;
///////////////////////////////////////////////////////////////////////
/**********************************************************************
* Add a Node to LLL; data Uninitialized
* Return: 1=succeed; 0=failed
**********************************************************************/
int
lll_add()
{
int i;
struct sysinfo si;
/**********************************************************************
* Allocating nodes according to the input space specified
* Assuming that input space is in KB, and free{ram+swap} > space +
LIMIT (which is currently 100 MB)
**********************************************************************/
int
lll_eat(long unsigned space)
{
struct sysinfo si;
struct sysinfo si2;
A lot of times, particularly with large memory allocations, the kernel won't actually assign you memory until you try to do something with it (i.e. read or write from it), which your code does not appear to do. Also, the kernel caches memory extremely aggressively, so if you free memory, thew kernel may not actually free it in case your app wants it again (of course if another app asks for it the kernel will give it to that app). I suggest you take a look at the vm sysctl variables and what they do for a further explanation.
Oh, and please put code in code tags -- makes it much easier to read.
Originally posted by Komakino How does it allocate all this space? Are you making multiple calls to 'malloc()' or similar? If you close the program without making the subsequent calls to free() (for the total amount of memory malloc'd) then the memory won't be freed back for other programs to use and so will remain in use.
This is not true. Every at least partially sane OS (like Linux) will free all memory dedicated to a process when that process dies.
Originally posted by addy86 This is not true. Every at least partially sane OS (like Linux) will free all memory dedicated to a process when that process dies.
I stand (or rather sit) corrected.
Was this the case at one time, or have I made the whole thing up?
"A lot of times, particularly with large memory allocations, the kernel won't actually assign you memory until you try to do something with it (i.e. read or write from it), which your code does not appear to do. "
=>It is true that malloc won't actually assigned the space until the return address from malloc is being write or read from. but Please look at lll_add() which is a a node into the linked list, when malloc passed, it would go into the else condition to create a node, as well as, WRITE a value 0, to all data, now you see that the code actually write value to ALL data for EVERY nodes.
"Also, the kernel caches memory extremely aggressively, so if you free memory, thew kernel may not actually free it in case your app wants it again (of course if another app asks for it the kernel will give it to that app)."
=>So why is it then the memory was freed after lll_destroy_all on RHEL3 or RHEL4 but not on kernel 2.6.13?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.