File: MemoryInfo.cs

package info (click to toggle)
sysinfo 0.7-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,852 kB
  • ctags: 1,161
  • sloc: cs: 2,933; sh: 701; makefile: 115; xml: 98
file content (173 lines) | stat: -rw-r--r-- 4,222 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Filename: MemoryInfo.cs
// Contains functions to extract information displayed in Memory category

using System;
using System.IO;

namespace Sysinfo {
	
	public class MemoryInfo {
		
		//global variables
		public String memory_total = "unknown";
		public String memory_free = "unknown";
		public String memory_free_total = "unknown";
		
		public String memory_swaptotal = "unknown";
		public String memory_swapfree = "unknown";
		
		public String memory_buffers = "unknown";
		public String memory_cached = "unknown";
		public String memory_active = "unknown";
		public String memory_inactive = "unknown";
		
		public Double fraction1, fraction2;
		
		//read memory info
		public void MemoryStaticInfo() {
			
			String temp;
			Boolean staticB = false;

			try {
				
				//get memory information from /proc
				using (TextReader textread = File.OpenText("/proc/meminfo")) {

					while ( staticB == false ) {
						
						temp = textread.ReadLine();
						
						//total
						if ( temp.StartsWith("MemTotal:")) {
							
							temp = temp.Remove(0, 9);
							temp = temp.Remove(temp.IndexOf("kB"), 2);
							
							memory_total = ( Int32.Parse(temp) / 1024 ).ToString();
						}
						
						//swap total
						if ( temp.StartsWith("SwapTotal:")) {
							
							temp = temp.Remove(0, 10);
							temp = temp.Remove(temp.IndexOf("kB"), 2);
							
							if ( temp == "0" )
								memory_swaptotal = "no swap";
							else
								memory_swaptotal = ( Int32.Parse(temp) / 1024 ).ToString();
							
							staticB = true;
						}
						
					}
				}
			}catch (FileNotFoundException ex) { Console.WriteLine( ex); }
		}
		
		//read memory info
		public void MemoryDynamicInfo() {
			
			String temp;
			Boolean dynamicB = false;

			try {
				
				//get memory information from /proc
				using (TextReader textread = File.OpenText("/proc/meminfo")) {

					while ( dynamicB == false ) {
						
						temp = textread.ReadLine();
						
						//free
						if ( temp.StartsWith("MemFree:")) {
							
							temp = temp.Remove(0, 8);
							temp = temp.Remove(temp.IndexOf("kB"), 2);
							
							memory_free = ( Int32.Parse(temp) / 1024 ).ToString();
						}
						
						//buffers
						if ( temp.StartsWith("Buffers:")) {
							
							temp = temp.Remove(0, 8);
							temp = temp.Remove(temp.IndexOf("kB"), 2);
							
							memory_buffers = ( Int32.Parse(temp) / 1024 ).ToString();
						}
						
						//swap free
						if ( temp.StartsWith("SwapFree:")) {
							
							temp = temp.Remove(0, 9);
							temp = temp.Remove(temp.IndexOf("kB"), 2);

							memory_swapfree = ( Int32.Parse(temp) / 1024 ).ToString();
							
							dynamicB = true;
						}
						
						//cached memory
						if ( temp.StartsWith("Cached:")) {
							
							temp = temp.Remove(0, 7);
							temp = temp.Remove(temp.IndexOf("kB"), 2);

							memory_cached = ( Int32.Parse(temp) / 1024 ).ToString();
						}
						
						//active memory
						if ( temp.StartsWith("Active:")) {
							
							temp = temp.Remove(0, 7);
							temp = temp.Remove(temp.IndexOf("kB"), 2);

							memory_active = ( Int32.Parse(temp) / 1024 ).ToString();
						}
						
						//inactive memory
						if ( temp.StartsWith("Inactive:")) {
							
							temp = temp.Remove(0, 9);
							temp = temp.Remove(temp.IndexOf("kB"), 2);

							memory_inactive = ( Int32.Parse(temp) / 1024 ).ToString();
						}

					}
				}
			}catch (FileNotFoundException ex) { Console.WriteLine( ex); }
			
			memory_free_total = ( Double.Parse(memory_free) + Double.Parse(memory_buffers) + Double.Parse(memory_cached) ).ToString();
		}
		
		
		//memory free progressbar
		public void Progressbar1() {
			
			try {
				
				fraction1 = Int32.Parse(memory_total) - Int32.Parse(memory_free_total);
				fraction1 = fraction1 / Int32.Parse(memory_total);
			}
			catch (DivideByZeroException ex) { fraction1 = 0;  Console.WriteLine( ex );  }
		}
		
		//swap free progressbar
		public void Progressbar2() {
			
			try {
				
				fraction2 =Int32.Parse(memory_swaptotal) - Int32.Parse(memory_swapfree);
				fraction2 = fraction2 / Int32.Parse(memory_swaptotal);
			}
			catch (DivideByZeroException ex) { fraction2 = 0;  Console.WriteLine( ex );  }
		}
		
	}
}

//ghaefb