Solved

How do I force the program to free memory?

Posted on 2001-09-11
8
246 Views
Last Modified: 2013-12-06
I'm using HP-UX.11 (but we have to port software to Sun and other platforms
so the solution must be portable too).

I have understood that when process allocates memory then it of course
gets it, but never really frees it (even when you call delete/free in your program).
So that memory is never released to other processes but kept reserved for
that single process. So when process calls delete the memory is not freed
to other process and when the same process calls new/malloc() the memory
is take from that memory space that has been reserved for that process.

Is there a way to force the program to release the memory or somehow
configure OS so that it releases the memory?
0
Comment
Question by:tonitop
  • 4
  • 3
8 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 6474343
You are correct in the way memory allocation works. When a process executes a malloc() to gain address space the system will use brk(), if necessary, to allocate a chunk of memory to the calling process. That allocated memory will remain a part of the address space for the life of the task and I know of no way to free memory allocated by brk() other than to exit the task.
0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 100 total points
ID: 6475026
It is possible to release memory allocated by brk() by calling
sbrk() with a negative increment.

But doing this requires compacting the memory so that the part
you're about to free isn't being used anymore.

If your program is recursive and therefore stack intensive, you might be able to manage memory yourself (and not ever ever ever call malloc)
through brk/sbrk.

Otherwise, compacting the memory will be very difficult and
very computationally expensive.  So expensive that it's not worth
doing at all.

Why?  Because the way the demanding paging system works, pags
that are not being used will be paged out anyway.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6475787
All about free said by the first 2 comments.

And it's still more worse than that, 'cause most OS don't have a useful garbage collection. So malloc slices the available memory for a process for most calls. Example:
     process allocated 100kbyte and 10kbyte
     process frees these 100kbyte and these 10kbytes (address space still belongs to the process)
     process tries to allocate 101kbytes, now it did not find a continous block of such size and allocates a new 101kbyte block to the process, and so on ...
Imagine a scenario when small blocks are required (like in most C++, or alike, programming languages) and the process is running for a long time, it comes to the point where the process has allocated a lot of memory which is unused by the process but cannot be used 'cause it is sliced down.

AFAIK, the only solution to avoid this is to use your own memory allocation, like chris_calabrese described.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6476665
What malloc are you using ahoffmann?  I know some older implementations worked like that, but most modern ones would collapse the 100kbyte block and the 10kbyte block into a single 110kbyte block.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 6477397
SunOS, Solaris <= 2.3, HP-UX <=9.x, AIX < 4.0, IRIX < 6.0 (probaly also OSF and SINIX)
Didn't check newer versions, so my information may be outdated now.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6477517
Hmm, I think Phong Vo made that change to the
Bell Labs malloc in around 1992, and that it
made it into sVr4.2 and also into the later BSD's.

Given that all these systems are based on earlier code
from USL and/or BSD, that's actually not too surprising.
This should be fixed in UnixWare, Linux, and the BSD's.

I'd be surprised if it isn't also in newer releases
of the OS' you mention too, though.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6477560
aha, I see I was not talking in miracles ;-)
and also that chris_calabrese seems to be much deeper in the UNIX sources than me.
I don't have UNIX's family tree in my brain, but slightly remember that SunOS (and so Solaris) is a very early branch.
And I think I never watched these symtomes on BSD systems (FreeBSD, NetBSD, Linux).
Nice to get new hints on this (ancient) item. Thanks.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6477588
SunOS 4.x is a pretty ancient branch, but most of Solaris comes from sVr4.1 MP.  Don't know if they took the compilers stuff, though.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now