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