Sunday 1 September 2024

Computers, Memory Pyramid & Code Size Optimization.

What is a Computer?

In Computer Sciences, Computer - by definition - is processor with memory and input/output devices. Any electronic device that has these is considered Computer. This includes Smartphones and many other tools.


Memory, Persistent or Transient.

There are two types of memory / pl: 'Są dwa rodzaje pamięci' /:
- Persistent / pl: 'Trwała' /,
- Transient / pl: 'Ulotna' /.

Persistent objects are those which continue to exist even after the program that created them has stopped running.

Transient objects cease to exist when program that created them stops.


Pyramid of Needs.

There are many types of memory, differing in price and speed of access.

Starting from the most expensive but fastest, there are:
- processor's registers,
- layers of the processor's cache (L1-L3, for example),
- RAM (Random Access Memory),
- persistent SSD/HDD storage.


Code Size optimization.

Smaller programs can be very quick in their execution.

When the whole program fits - for example - in L2 Processor's Cache, there's no need to reach RAM via BUS, so the code runs very quickly - as it's closer to the processor than RAM.


What if a Program doesn't fit in Transient Memory?

When a program needs to be executed, it needs to be loaded into the transient memory first.

However, Modern Operating Systems can send currently unused Program's parts & other Resources / for example: graphics image files, sound files and/or text files / from Transient Memory to Persistent SDD/HDD Memory and retrieve other Resources/Part(s) from Persistent Memory to Transient Memory / Usually from disk to RAM /.

/ pl: 'Współczesne Systemy Operacyjne mogą wysłać aktualnie niewykorzystywane części Oprogramowania i innych zasobów na dysk... i sprowadzić inne zasoby/części z pamięci trwałej do ulotnej, najczęściej do pamięci RAM' /.

Let's note, however, that loading/storing data in persistent memory is much slower than loading/storing from/to Transient RAM.

This is an automated operation in Modern Operating Systems, so programmers do not need to worry so much about that. Computers just slow down sometimes - and SSD/HDD becomes quite busy, when doing that.


This sometimes causes 'Flickering' / pl: 'Migotanie' /, however. Code & Data is loaded/unloaded from/to persistent memory too slowly and can cause a Computer System to slow down or crash, as the Computing Resources run out.
/ Mostly CPU usage & Memory usage /.

So - in theory at least - a Computer can try to run larger programs than Computer has RAM.

Often it fails, but in theory this can work well.