Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMDS / SMDS_MemoryLimit.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : SMDS_MemoryLimit.cxx
23 // Created   : Fri Sep 21 17:16:42 2007
24 // Author    : Edward AGAPOV (eap)
25 // Executable to find out a lower RAM limit (MB), i.e. at what size of freeRAM
26 // reported by sysinfo, no more memory can be allocated.
27 // This is not done inside a function of SALOME because allocated memory is not returned
28 // to the system. (PAL16631)
29 //
30 #ifndef WIN32
31 #include <sys/sysinfo.h>
32 #endif
33
34 #ifdef _DEBUG_
35 #include <iostream>
36 #endif
37
38 int main (int argc, char ** argv)
39 {
40   // To better understand what is going on here, consult bug [SALOME platform 0019911]
41 #ifndef WIN32
42   struct sysinfo si;
43   int err = sysinfo( &si );
44   if ( err )
45     return -1;
46   unsigned long freeRamKb = ( si.freeram  * si.mem_unit ) / 1024;
47
48   // totat RAM size in Gb, float is in order not to have 1 instead of 1.9
49   float totalramGb = float( si.totalram * si.mem_unit ) / 1024 / 1024 / 1024;
50
51   // nb Kbites to allocate at one step. Small nb leads to hung up
52   const int stepKb = int( 5 * totalramGb );
53
54   unsigned long nbSteps = freeRamKb / stepKb * 2;
55   try {
56     while ( nbSteps-- ) {
57       new char[stepKb*1024];
58       err = sysinfo( &si );
59       if ( !err )
60         freeRamKb = ( si.freeram  * si.mem_unit ) / 1024;
61     }
62   } catch (...) {}
63
64 // #ifdef _DEBUG_
65 //   std::cout << freeRamKb / 1024 << std::endl;
66 // #endif
67   return freeRamKb / 1024;
68
69 #endif
70
71   return -1;
72 }