Types and Literals

Memory Usage in Modern Computers

Why do we need to care about memory usage in modern computers when they have so much RAM available?

3D art representing computer programming

Even though modern computers have lots of memory, being efficient with memory usage is still important for several reasons:

First, your program isn't running alone. Modern computers might be running dozens or even hundreds of programs at once. If every program wastes memory, it adds up quickly. Think of it like sharing a pizza - if everyone takes more slices than they need, some people might not get any!

Second, some programs need to handle huge amounts of data. Imagine a game that needs to track thousands of monsters, each with their own stats. If we waste even a small amount of memory per monster, it can add up to a lot of wasted space:

#include <iostream>
using namespace std;

int main() {
  // Using too much memory per monster
  int64_t Health{100};  // 8 bytes
  int64_t Level{1};     // 8 bytes
  // Total: 16 bytes per monster

  // Using appropriate sized types
  int16_t BetterHealth{100};  // 2 bytes
  int8_t BetterLevel{1};      // 1 byte
  // Total: 3 bytes per monster

  int NumberOfMonsters{10000};

  cout << "Wasteful approach uses: "
    << NumberOfMonsters * 16 << " bytes\n";
  cout << "Efficient approach uses: "
    << NumberOfMonsters * 3 << " bytes\n";
}
Wasteful approach uses: 160000 bytes
Efficient approach uses: 30000 bytes

Third, some devices (like phones or game consoles) have limited memory. If you want your program to work well on these devices, you need to be careful with memory.

Finally, using more memory than necessary can make your program slower. When your program uses memory, the computer needs to manage that memory - allocating it, tracking it, and cleaning it up. The more memory you use, the more work the computer has to do.

Think of memory usage like packing for a trip - even if you have a huge suitcase, it's still better to pack efficiently. It makes everything easier to manage, and leaves room for when you really need it!

This Question is from the Lesson:

Types and Literals

Explore how C++ programs store and manage numbers in computer memory, including integer and floating-point types, memory allocation, and overflow handling.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Types and Literals

Explore how C++ programs store and manage numbers in computer memory, including integer and floating-point types, memory allocation, and overflow handling.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved