# Defrag option in Sun Solaris.



## Rajini (Apr 25, 2008)

Hi All,

I am new to unix and in the process of learning the concepts.

Is there a way to perform Memory defragmentation in Unix?

If there is no option then please explain how memory is handled in a Unix environment and how free space is collected.

Regards,
Rajini


----------



## lotuseclat79 (Sep 12, 2003)

Hi Rajini,

Memory allocation takes place at a couple of levels:
1) when the OS initiates a new process/or thread to execute a program or command, and
2) when a program invokes memory allocation primitives within the name space of the program

Two kinds of memory leaks can occur - at the system level - and the more common memory leaks where program allocated memory is not returned to the memory pool including (leaked threads and open files).

The common solution is a garbage collector which takes care of recycling unreferenced memory.

For C and C++ programs (which do not have garbage collection), one can install the Boehm-Demers- Weiser conservative garbage collector from here.

As for any system/thread level memory leaks, that may be a concern for the kernel. For example, there may be a question about unix domain sockets requiring a mark & sweep garbage collection algorithm - see Unix garbage collection. It is unclear to me from reading that post whether "the problem" exists in Linux and Solaris or Linux and Solaris have such an approach to handling the problem whereas Unix does not.

An interesting concept is discussed in GCFS: a Garbage-Collected Filesystem for Linux.

More on GC here.

-- Tom


----------



## Rajini (Apr 25, 2008)

Hi Tom,

Thanks for the information ,it was useful in understanding few internal concepts on how memory is handled.

Based on your reply ,I have few clarifications....

Two kinds of memory leaks can occur - at the system level - and the more common memory leaks where program allocated memory is not returned to the memory pool including (leaked threads and open files)
<Rajini> Is there a in-built command\process available in "SUN Solaris" which does Garbage collection and takes care of Memory leaks.

The reason I am curious is ,if there is no in-built option available then the only option which I guess for the Server to reclaim the memory would be after a server re-boot.(Correct me I am wrong....)

Regards,
Rajini


----------



## lotuseclat79 (Sep 12, 2003)

Rajini said:


> Hi Tom,
> 
> Thanks for the information ,it was useful in understanding few internal concepts on how memory is handled.
> 
> ...


Hi Rajini,

Usually, servers are not general purpose, e.g. a dedicated data base server host, or an Apache web server running on a web host (other servers may also be running).

Some hosts (nodes on networks), have more than one special purpose server running.

It all depends on whether the memory leak was caused by poor programming at the user level - in which case, when the command is exited, the system will clean up its use of local memory allocated to it. While the program (command) is running, then if it was compiled with a garbage collecting library - the GC will take care of memory cleanup dynamically while the program (server) is still running.

Then there is the OS which takes care of process level cleanup after command (programs or servers) are exited.

A server reboot does not have to mean a total system OS reboot, just an exit for the server, and then re-launch it.

No one system level garbage collector is needed. Each command or server probably manages its own allocated memory - unless it has an unknown bug that occurs, and the server needs to be rebooted (not the entire system).

-- Tom


----------

