Salome HOME
[SALOME platform 0019911]: pb of memory allocation in SMESH
[modules/smesh.git] / src / SMDS / SMDS_MemoryLimit.cxx
1 // File      : SMDS_MemoryLimit.cxx
2 // Created   : Fri Sep 21 17:16:42 2007
3 // Author    : Edward AGAPOV (eap)
4
5 // Executable to find out a lower RAM limit (MB), i.e. at what size of freeRAM
6 // reported by sysinfo, no more memory can be allocated.
7 // This is not done inside a function of SALOME because allocated memory is not returned
8 // to the system. (PAL16631)
9
10 #ifndef WIN32
11 #include <sys/sysinfo.h>
12 #endif
13
14 #ifdef _DEBUG_
15 #include <iostream>
16 #endif
17
18 int main (int argc, char ** argv)
19 {
20   // To better understand what is going on here, consult bug [SALOME platform 0019911]
21 #ifndef WIN32
22   struct sysinfo si;
23   int err = sysinfo( &si );
24   if ( err )
25     return -1;
26   unsigned long freeRamKb = ( si.freeram  * si.mem_unit ) / 1024;
27
28   // totat RAM size in Gb, float is in order not to have 1 instead of 1.9
29   float totalramGb = float( si.totalram * si.mem_unit ) / 1024 / 1024 / 1024;
30
31   // nb Kbites to allocate at one step. Small nb leads to hung up
32   const int stepKb = int( 5 * totalramGb );
33
34   unsigned long nbSteps = freeRamKb / stepKb * 2;
35   try {
36     while ( nbSteps-- ) {
37       new char[stepKb*1024];
38       err = sysinfo( &si );
39       if ( !err )
40         freeRamKb = ( si.freeram  * si.mem_unit ) / 1024;
41     }
42   } catch (...) {}
43
44 // #ifdef _DEBUG_
45 //   std::cout << freeRamKb / 1024 << std::endl;
46 // #endif
47   return freeRamKb / 1024;
48
49 #endif
50
51   return -1;
52 }