Salome HOME
0023544: SMESH's performance issues
[modules/smesh.git] / src / SMDS / SMDS_MemoryLimit.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, 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 #ifdef _DEBUG_
33 #include <iostream>
34 #endif
35
36 int main (int argc, char ** argv)
37 {
38   // To better understand what is going on here, consult bug [SALOME platform 0019911]
39 #if !defined WIN32 && !defined __APPLE__
40   struct sysinfo si;
41   int err = sysinfo( &si );
42   if ( err )
43     return -1;
44   unsigned long freeRamKb = ( si.freeram  * si.mem_unit ) / 1024;
45
46   // totat RAM size in Gb, float is in order not to have 1 instead of 1.9
47   float totalramGb = float( si.totalram * si.mem_unit ) / 1024 / 1024 / 1024;
48
49   // nb Kbites to allocate at one step. Small nb leads to hung up
50   const int stepKb = int( 5 * totalramGb );
51
52   unsigned long nbSteps = freeRamKb / stepKb * 2;
53   try {
54     while ( nbSteps-- ) {
55       new char[stepKb*1024];
56       err = sysinfo( &si );
57       if ( !err )
58         freeRamKb = ( si.freeram  * si.mem_unit ) / 1024;
59     }
60   } catch (...) {}
61
62 // #ifdef _DEBUG_
63 //   std::cout << freeRamKb / 1024 << std::endl;
64 // #endif
65   return freeRamKb / 1024;
66
67 #endif
68
69   return -1;
70 }