Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I force the program to free memory?

Posted on 2001-09-11
8
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

730 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