casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Memory.h
Go to the documentation of this file.
00001 //# Memory.h: Memory related information and utilities.
00002 //# Copyright (C) 1997,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: Memory.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00028 
00029 #ifndef CASA_MEMORY_H
00030 #define CASA_MEMORY_H
00031 
00032 #include <casa/aips.h>
00033 //# The following is used to get size_t.
00034 #include <casa/stdlib.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // <summary>Memory related information and utilities.</summary>
00039 
00040 // use visibility=export>
00041 
00042 // <reviewed reviewer="rmarson" date="1997/11/12" tests="tMemory" demos="">
00043 // </reviewed>
00044 
00045 // <prerequisite>
00046 //   <li> General knowledge of C/C++ memory allocation issues.
00047 // </prerequisite>
00048 //
00049 // <synopsis>
00050 // This class should generally not be used by general application programmers.
00051 // Instead you should use the memory information available in the 
00052 // <linkto class=AppInfo>AppInfo</linkto> class.
00053 //
00054 // This class reports on the dynamic ("heap") memory used by this process, and
00055 // it can attempt to release some of that memory back to the operating
00056 // system. The class reports on <src>allocated</src> memory, which is memory
00057 // that the process has actually requested, typically via <src>new</src>, but
00058 // also via <src>malloc</src>. The number might be somewhat larger than actually
00059 // requested by the programmer to account for overhead and alignment
00060 // considerations. The class also reports <src>assigned</src> memory, which is
00061 // the total memory that the process has been given, not all of which has been
00062 // allocated by the programmer. Typically this results from memory which has
00063 // been <src>delete</src>d by the programmer, but has not been released to the
00064 // OS on the assumption that it might be needed again. (Getting and releasing
00065 // memory to the OS can be expensive).
00066 //
00067 // At present, the values for allocated and assigned memory are obtained via the
00068 // <src>mallinfo()</src> call, usually defined in <src>malloc.h</src>. This call
00069 // seems to be adequately portable for Unix. Another alternative would be to
00070 // replace global operators <src>new</src> and <src>delete</src> for systems
00071 // that do not have <src>mallinfo()</src>.
00072 //
00073 // The member function <src>releaseMemory()</src> on some system will attempt
00074 // to return assigned but unallocated memory to the OS. If the compilation
00075 // system cannot automatically determine how to do this (at present it only
00076 // recognizes Linux systems), you can you this function by setting the
00077 // preprocessor symbol <src>AIPS_RELEASEMEM</src> to a C++ statement which
00078 // will release memory to the OS. For example, if you are using GNU malloc
00079 // you could set it to <src>malloc_trim(0)</src>. Note that
00080 // <src>releaseMemory()</src> might be a no-op on many systems, and that
00081 // calling it might be expensive so it should not be called in tight-loops.
00082 //
00083 // Note that this class does not use any AIPS++ facilities and does not cause
00084 // any AIPS++ code to be linked in to your executable.
00085 // </synopsis>
00086 //
00087 // <example>
00088 // We could attempt to return memory to the OS when we are wasting a lot
00089 // of memory as follows. 
00090 // <srcBlock>
00091 // if (Memory::assignedMemoryInBytes() - Memory::allocatedMemoryInBytes() >
00092 //     1024*1024) {
00093 //     Memory::releaseMemory(); // Attempt to release if more than 1M "wasted"
00094 // }
00095 // </srcBlock>
00096 // </example>
00097 //
00098 // <motivation>
00099 // At run time we need to be able to make decisions about whether we should
00100 // keep things in memory or about whether we should do I/O (e.g. use an
00101 // Array or a PagedArray).
00102 // </motivation>
00103 //
00104 // <todo asof="1997/11/10">
00105 //   <li> We might some day want to put actual allocation/deallocation
00106 //        functions in here if the mallinfo() interface turns out to not
00107 //        be portable.
00108 // </todo>
00109 
00110 class Memory
00111 {
00112 public:
00113     // How much memory has been allocated by the programmer (via either
00114     // <src>new</src> or <src>malloc</src>. This can include some extra
00115     // overhead bytes, e.g. for alignment reasons.
00116     static size_t allocatedMemoryInBytes();
00117     // How much memory in in the memory "pool" of this process. This includes
00118     // not only the allocated memory, but some memory which has been
00119     // <src>delete</src>d but not returned to the OS on the assumption that
00120     // it might be needed again.
00121     static size_t assignedMemoryInBytes();
00122     // Attempt to release memory which has been assigned but not allocated.
00123     // On many systems this will be a no-op, and even on systems in which it
00124     // does something the amount of reclaimed memory cannot be specified.
00125     // Since this function may be somewhat expensive to call it should not
00126     // be called too often.
00127     static void releaseMemory();
00128 
00129     // setMemoryOptions and setMemoryOption are typically front ends for mallopt
00130     // which lets the user control some memory allocation parameters.  setMemoryOptions
00131     // is intended to be called only once at the start of a program while setMemoryOption
00132     // could be called where desired (but see mallopt man page for possible side effects).
00133     // Note: these two functions were added to address in a general way a memory
00134     // fragmentation problem encountered on by the MIPSpro C++ compiler on the SGI.
00135     static void setMemoryOptions();
00136     static int  setMemoryOption(int, int);
00137 };
00138 
00139 
00140 } //# NAMESPACE CASA - END
00141 
00142 #endif
00143 
00144