Bit , Bytes And Memory Management

vishal rana
8 min readJan 4, 2023

--

Image from google

Every software developer should understand what is a bit, bytes and how memory management works in computer programs. Bits, bytes, and memory management are important concepts in computer science that play a critical role in how computers store, process, and access data.

A bit (short for “binary digit”) is the smallest unit of data in a computer. It is a binary value that can be either 0 or 1, and is used to represent the two possible states of a digital circuit.

A byte is a unit of data that consists of 8 bits. It is often used to represent a character of text or a small piece of data, such as a number or a symbol.

Memory management is the process of allocating, organizing, and managing the memory of a computer in order to optimize its performance and efficiency. It involves techniques such as paging and virtual memory, which allow a computer to store and access data from its memory more efficiently.

Let’s try to understand them in more depth

Bit

A bit (short for “binary digit”) is the smallest unit of data in a computer. It is a binary value that can be either 0 or 1, and is used to represent the two possible states of a digital circuit.

For example, a bit can be used to represent a switch that is either on or off, or a light that is either on or off.

Bits are the fundamental building blocks of digital information, and are used to represent data in a computer’s memory. They are used to store and transmit information in a variety of forms, including text, images, audio, and video.

Bytes

Bits are usually organized into larger units called bytes, which consist of 8 bits. A byte is often used to represent a single character of text or a small piece of data, such as a number or a symbol.

Bits are also used to represent data in different formats, such as ASCII (American Standard Code for Information Interchange) and Unicode. ASCII is a widely used standard for representing text in computers, and uses 7 bits to represent each character. Unicode is a more comprehensive standard that uses 16 bits to represent each character, and includes support for a wide range of languages and symbols.

You might have heard about 32 bits system and 64 bits system. A 32-bit system is a computer that is designed to process data in 32-bit chunks, while a 64-bit system is a computer that is designed to process data in 64-bit chunks.

Here are a few examples of how bytes are used in practice:

  • Storing text: In many computer systems, a byte(8 bits) is used to represent a single character of text. For example, the letter “A” might be represented as the byte 01000001, while the letter “B” might be represented as 01000010.
  • Storing numbers: Bytes can also be used to represent numbers. For example, the number 255 might be represented as 11111111, while the number 128 might be represented as 10000000.
  • Storing flags: Bytes can be used to represent flags that have two possible values, such as true or false. For example, a byte might be used to represent a flag that indicates whether a particular feature is enabled or disabled.
  • Storing images: Images are often stored in a computer as a series of pixels, where each pixel is represented by a number of bytes. For example, an image might be stored as a series of bytes that represent the color and intensity of each pixel in the image.
range of values for each bit used

As you can see, the range of values that can be stored in a given number of bits increases as the number of bits increases. For example, a 1-bit value can only represent two possible values (0 or 1), while a 32-bit value can represent over 4 billion possible values.

As you have used 128 bits and 256 bits encryption protocols , that is the reason they are very hard to hack as there are billions and billions of possible combinations that will take years to decode them.

binary representation of numbers from 1 to 10

As you can see in above image, only 4 bits are used to represent numbers from 1 to 10.

Let’s try to understand with a real world example of storing an object:

let person= {
"id": 123456789,
"name" : "Vishal",
"gender": "male",
"onPayroll": true,
"elite": true,
"hobbies": ["reading", "writing"],
"address": {
"street": "Somewhere",
"country": "India"
}
}

Explanation

“id” is a number that takes up 4 bytes (32 bits) of memory to store, as it is a 32-bit integer. “name” is a string of text that takes up a variable amount of memory to store, depending on its length. In this case it is of length 6 so we need 6 bytes(8 bits for each character) to store this. “gender” is also string, and they also take up a variable amount of memory. In this case 4 bytes.

“onPayroll” and “elite” are Boolean values that can be either true or false, and they each take up 1 byte (8 bits) of memory to store. “hobbies” is an array of strings, and it takes up a variable amount of memory to store depending on the number of strings it contains.

“address” is an object that contains two string properties (“street” and “country”), and it takes up a variable amount of memory to store depending on the length of these strings.

Memory Management

Memory is a term that refers to the storage areas in a computer system where data is held temporarily or permanently.

Memory management is the process of managing a computer’s memory in order to optimize its performance and make the best use of available resources. It involves allocating memory to different programs and processes as needed, and deallocating memory when it is no longer needed. This can be a powerful approach, as it allows the programmer to have fine-grained control over how memory is used. While working with C you might have used alloc and dealloc functions that is used to allocate and deallocate memory to store values.

Although , you need to make sure if you allocate memory you need to deallocate it(when it is no longer needed) as well, otherwise it will leads to memory leakage.

Most of the modern programming language does this automatically as they have concept of garbage collection working for them on auto pilot.

Garbage collector

Garbage collection is an automated approach to memory management that involves a garbage collector tracking the usage of memory resources and automatically freeing resources that are no longer in use. This can be a convenient approach, as it frees the programmer from having to manually track and free memory resources.

However, it can also introduce overhead, as the garbage collector must continuously track the usage of memory resources, which can impact performance.

Reference counting

Reference counting is an approach to memory management that involves keeping track of the number of references to a memory resource, and automatically freeing the resource when the reference count reaches zero. This can be a simple and efficient approach to memory management, as it requires minimal overhead.

However, it can be prone to errors, as it relies on the programmer accurately keeping track of the reference count of each memory resource.

Memory management is an important consideration for software developers, as it can have a significant impact on the performance and reliability of a system

Type of memory

  • Primary memory
  • Secondary memory

Primary Memory:

Primary memory, also known as main memory or internal memory, refers to the memory that is directly accessible to the CPU (Central Processing Unit)

Primary memory is volatile, which means that it is wiped clean when the power is turned off. Examples of primary memory include RAM (Random Access Memory) and cache memory

Secondary Memory:

Secondary memory, also known as external memory or auxiliary memory, refers to storage devices that are not directly accessible to the CPU.

Secondary memory is non-volatile, which means that it retains its data even when the power is turned off. Examples of secondary memory include hard drives, solid state drives, and USB drives.

Primary memory is typically faster and more expensive than secondary memory, and is used to store data and instructions that are actively being used or processed by the system.

Whenever you try to run any application on your system, it is first loaded on the primary memory and then its execution takes place. Once primary memory reaches a certain limit , a virtual memory is being used, which is nothing but secondary memory.

Virtual memory allows a system to run programs that require more memory than is physically available by temporarily transferring pages of data from RAM to the hard disk when they are not being used. When the program needs to access the data again, it is transferred back to RAM.

There are several key techniques and strategies that are used in memory management, including:

  • Memory allocation: This involves assigning blocks of memory to different programs and processes as needed. Memory allocation can be static, where memory is allocated in advance, or dynamic, where memory is allocated on demand as the program or process runs.
  • Memory deallocation: This involves releasing memory back to the system when it is no longer needed. Memory deallocation can be manual, where the programmer must explicitly free memory when it is no longer needed, or automatic, where the system automatically releases memory when it is no longer needed.
  • Memory fragmentation: This occurs when the available memory is split into small, non-contiguous blocks, making it difficult to allocate large blocks of memory when needed. Memory fragmentation can be addressed by using techniques such as compaction, which moves blocks of memory together to create larger blocks of contiguous memory.
  • Memory leakage: This refers to the situation where a program or process allocates memory but fails to release it when it is no longer needed, resulting in a gradual reduction in available memory. Memory leakage can be prevented by using techniques such as garbage collection, which automatically releases memory that is no longer needed.

I hope this helps you understand basics. If you like to read content like this, do checkout my other blogs:

Other Life changing blogs for productivity and Focus:

--

--