@@ -14,9 +14,9 @@ namespace DotnetSpider.Infrastructure
1414{
1515 public struct MemoryStatus
1616 {
17- public int FreeMemory ;
18- public int UsedMemory ;
19- public int TotalMemory ;
17+ public long FreeMemory ;
18+ public long UsedMemory ;
19+ public long TotalMemory ;
2020 }
2121
2222 public static class SystemInformation
@@ -26,7 +26,7 @@ private static class OSX
2626 {
2727 private static readonly int _structLength = Marshal . SizeOf < VmStatistics > ( ) ;
2828 private static readonly int _pageSize ;
29- private static readonly int _totalMemory ;
29+ private static readonly long _totalMemory ;
3030
3131 static OSX ( )
3232 {
@@ -50,20 +50,17 @@ public static MemoryStatus GetMemoryStatus()
5050 statistics . compressor_page_count ) *
5151 _pageSize ) / 1024 / 1024 ;
5252
53- return new MemoryStatus
54- {
55- TotalMemory = _totalMemory , UsedMemory = ( int ) ( used ) , FreeMemory = ( int ) ( free )
56- } ;
53+ return new MemoryStatus { TotalMemory = _totalMemory , UsedMemory = ( used ) , FreeMemory = ( free ) } ;
5754 }
5855
5956 /// <summary>
6057 /// MB
6158 /// </summary>
6259 /// <returns></returns>
63- private static int GetTotalMemory ( )
60+ private static long GetTotalMemory ( )
6461 {
6562 var n = GetNumber ( "hw.memsize" ) ;
66- return ( int ) ( n / 1024 / 1024 ) ;
63+ return ( n / 1024 / 1024 ) ;
6764 }
6865
6966 [ DllImport ( "libc" , CallingConvention = CallingConvention . Cdecl ) ]
@@ -166,7 +163,7 @@ private static long GetNumber(string param)
166163 private static class Windows
167164 {
168165 private static readonly uint _structLength = ( uint ) Marshal . SizeOf < WindowsMemoryStatus > ( ) ;
169- private static readonly int _totalMemory ;
166+ private static readonly long _totalMemory ;
170167
171168 static Windows ( )
172169 {
@@ -188,11 +185,11 @@ public static MemoryStatus GetMemoryStatus()
188185 } ;
189186 }
190187
191- private static int GetTotalMemory ( )
188+ private static long GetTotalMemory ( )
192189 {
193190 var memoryInfo = new WindowsMemoryStatus { DwLength = _structLength } ;
194191 GlobalMemoryStatusEx ( ref memoryInfo ) ;
195- return memoryInfo . Equals ( default ) ? 0 : ( int ) ( memoryInfo . UllTotalPhys / 1024 / 1024 ) ;
192+ return memoryInfo . Equals ( default ) ? 0 : ( memoryInfo . UllTotalPhys / 1024 / 1024 ) ;
196193 }
197194
198195 [ StructLayout ( LayoutKind . Sequential , Pack = 1 ) ]
@@ -217,7 +214,7 @@ private struct WindowsMemoryStatus
217214 private static extern bool GlobalMemoryStatusEx ( ref WindowsMemoryStatus mi ) ;
218215 }
219216
220- private static class Linux
217+ internal static class Linux
221218 {
222219 /// <summary>
223220 /// eg. "MemTotal: 12345 kB"
@@ -227,40 +224,71 @@ private static class Linux
227224 RegexOptions . Multiline | RegexOptions . Compiled
228225 ) ;
229226
230- private static readonly int _totalMemory ;
227+ private static readonly long _totalMemory ;
231228
232229 static Linux ( )
233230 {
234- var dict = GetDict ( ) ;
235- _totalMemory = ( int ) dict [ "MemTotal" ] ;
231+ _totalMemory = GetTotalMemory ( GetMeminfo ( ) ) ;
236232 }
237233
238234 public static MemoryStatus GetMemoryStatus ( )
239235 {
240- var dict = GetDict ( ) ;
241- var free = ( int ) dict [ "MemAvailable" ] ;
236+ var free = GetFreeMemory ( GetMeminfo ( ) ) ;
242237 return new MemoryStatus
243238 {
244239 TotalMemory = _totalMemory , FreeMemory = free , UsedMemory = _totalMemory - free
245240 } ;
246241 }
247242
248- private static IDictionary < string , ulong > GetDict ( )
243+ internal static long GetTotalMemory ( string output )
249244 {
250- var path = "/proc/meminfo" ;
251- if ( ! File . Exists ( path ) )
245+ if ( string . IsNullOrWhiteSpace ( output ) )
252246 {
253- return default ;
247+ return 0 ;
248+ }
249+
250+ var dict = GetDict ( output ) ;
251+ var total = dict [ "MemTotal" ] ;
252+ return total ;
253+ }
254+
255+ internal static long GetFreeMemory ( string output )
256+ {
257+ if ( string . IsNullOrWhiteSpace ( output ) )
258+ {
259+ return 0 ;
254260 }
255261
256- var lines = File . ReadAllText ( path ) ;
262+ var dict = GetDict ( output ) ;
263+ var free = dict [ "MemAvailable" ] ;
264+ return free ;
265+ }
257266
267+ internal static IDictionary < string , long > GetDict ( string output )
268+ {
269+ if ( string . IsNullOrWhiteSpace ( output ) )
270+ {
271+ return new Dictionary < string , long > ( ) ;
272+ }
273+
274+ var lines = output ;
258275 // ReSharper disable once RedundantEnumerableCastCall
259276 var dict = _linuxMemoryInfoLineRegex . Matches ( lines ) . Cast < Match > ( ) . ToDictionary ( match =>
260277 match . Groups [ "name" ] . Value . TrimStart ( )
261- , match => ulong . Parse ( match . Groups [ "value" ] . Value ) * 1024 ) ;
278+ , match => long . Parse ( match . Groups [ "value" ] . Value ) / 1024 ) ;
262279 return dict ;
263280 }
281+
282+ private static string GetMeminfo ( )
283+ {
284+ var path = "/proc/meminfo" ;
285+ if ( ! File . Exists ( path ) )
286+ {
287+ return default ;
288+ }
289+
290+ return File . ReadAllText ( path ) ;
291+ }
264292 }
265293
266294 public static async Task < double > GetCpuUsageForCurrentProcess ( )
0 commit comments