Asking the computer
What system is this? How many processors? How much memory does my program use? Which programs are running? This lesson asks the computer with Environment, RuntimeInformation, Process, GC and the file system.
What you will learn#
- the operating system and the .NET runtime,
- processors, memory, drives and the user,
- the current process and the list of all processes,
- the .NET memory manager (
GC), - writing a program that behaves differently on Windows and on other systems.
The program#
# asking the computer - the operating system, the runtime, processors, memory, drives and running programs
load "dotnet.ring"
using {
"System"
"System.IO"
"System.Diagnostics"
"System.Text"
"System.Runtime.InteropServices"
}
func main
oEnvironment = new Environment()
oRuntime = new RuntimeInformation()
# 1. Which system runs the program? A program that must behave differently on Windows, Linux and macOS asks first.
? "1. platform : " + oEnvironment.OSVersion.Platform
? " runtime : " + oRuntime.FrameworkDescription
? " process type : " + oRuntime.ProcessArchitecture + ", 64-bit system? " + yesNo(oEnvironment.Is64BitOperatingSystem)
? " is it Windows? : " + yesNo(runsOnWindows(oEnvironment))
# 2. The machine: processors and how long it has been running
? nl + "2. logical processors: " + oEnvironment.ProcessorCount
nSeconds = floor(oEnvironment.TickCount64 / 1000)
? " the computer has been on for more than a second: " + yesNo(nSeconds > 1)
# 3. Who and where: do not print private data in logs, but a program can use it (for example to find the user's folders)
? nl + "3. the user name and the machine name exist: " + yesNo(len(oEnvironment.UserName) > 0 and len(oEnvironment.MachineName) > 0)
? " the current folder exists: " + yesNo(new Directory().Exists(oEnvironment.CurrentDirectory))
# 4. Drives
aDrives = new Directory().GetLogicalDrives().toList()
? nl + "4. there is at least one drive: " + yesNo(len(aDrives) > 0) + ", and its name contains a colon (like C:): " + yesNo(substr(aDrives[1],":") > 0)
# 5. This program as a process: its id, its name, how much memory it uses
oProcess = new Process().GetCurrentProcess()
nMegabytes = floor(oProcess.WorkingSet64 / 1048576)
? nl + "5. this program is the process '" + oProcess.ProcessName + "'; it uses between 1 and 2000 MB: " + yesNo(nMegabytes >= 1 and nMegabytes < 2000)
# 6. .NET's memory manager: the memory used by .NET objects, and what happens after a collection
oGC = new GC()
nBefore = oGC.GetTotalMemory(False)
oBig = new StringBuilder(5000000)
nWith = oGC.GetTotalMemory(False)
? nl + "6. a StringBuilder with 5 million characters made the .NET memory grow: " + yesNo(nWith > nBefore + 1000000)
# 7. All programs that run now
aProcesses = new Process().GetProcesses(NULL).toList()
? nl + "7. more than five programs are running: " + yesNo(len(aProcesses) > 5)
lFound = False
for oOther in aProcesses
if oOther.Id = oProcess.Id
lFound = True
ok
next
? " this program is among them: " + yesNo(lFound)
# 8. A timer you can trust for measuring: Stopwatch uses the high resolution counter of the processor
? nl + "8. Stopwatch has a high resolution counter: " + yesNo(new Stopwatch().IsHighResolution)
# true when the program runs on Windows (the platform is "Win32NT" there, "Unix" on Linux and macOS)
func runsOnWindows oEnvironment
cPlatform = oEnvironment.OSVersion.Platform
return cPlatform = "Win32NT"
func yesNo lValue
if lValue
return "yes"
ok
return "no"1. platform : Win32NT
runtime : .NET 9.0.15
process type : X64, 64-bit system? yes
is it Windows? : yes
2. logical processors: 20
the computer has been on for more than a second: yes
3. the user name and the machine name exist: yes
the current folder exists: yes
4. there is at least one drive: yes, and its name contains a colon (like C:): yes
5. this program is the process 'ring'; it uses between 1 and 2000 MB: yes
6. a StringBuilder with 5 million characters made the .NET memory grow: yes
7. more than five programs are running: yes
this program is among them: yes
8. Stopwatch has a high resolution counter: yesThe output shows what is true on the computer where the page was made; the program prints yes and no for private details instead of the names themselves.
How it works#
The system and the runtime#
Environment.OSVersion.Platform is Win32NT on Windows and Unix on Linux and macOS. RuntimeInformation.FrameworkDescription is the .NET version and ProcessArchitecture the type of the processor (X64, Arm64...). A program that needs a different path or command on each system asks first and then chooses (the tutorial Paths and the environment shows paths that work everywhere).
The machine and the user#
ProcessorCount is the number of logical processors; it tells you how many tasks can really run at the same time (the tutorial Time, tasks and other programs). TickCount64 is the number of milliseconds since the system started. UserName and MachineName can be useful for the user's own folders or settings, but do not write them into logs or send them anywhere without a reason.
Drives#
Directory.GetLogicalDrives() lists the drives (C:\, D:\, ...; on Linux just /). For the size and the free space of a drive use DriveInfo (the tutorial Disks and free space).
This process and the others#
Process.GetCurrentProcess() describes the running program: Id, ProcessName (the program is the Ring interpreter, ring) and WorkingSet64, the memory it uses in bytes. Process.GetProcesses(NULL) is the list of all programs; the program finds itself in it by the Id. Starting and stopping other programs is in the tutorial Time, tasks and other programs and the tutorial A project: a command runner.
The memory manager#
.NET cleans up its own objects with the garbage collector (GC). GC.GetTotalMemory(False) is the memory used by .NET objects right now. Step 6 creates a StringBuilder with room for five million characters and the number grows by more than a megabyte. You almost never call the collector yourself; resources such as files need Dispose (the tutorial Releasing resources).
Time#
Stopwatch.IsHighResolution says that the processor has a precise counter, the right tool for measuring (the tutorial Speed — measure first).
Some functions return values that are different on every computer, so a test cannot compare them with a number. Test properties instead: the number is positive, the folder exists, this program is in the list.
What you learned#
Environment,RuntimeInformation,Process.GetCurrentProcessandGetProcesses,GC.GetTotalMemory,- testing values that differ between computers.
Exercises#
- Print the memory used by your program (in MB) before and after you fill a list with a million numbers.
- Show the 10 programs that use the most memory (name and MB) — a text task manager.
- Read the environment variable
PATHand print each folder on its own line (the tutorial Paths and the environment).