Concepts / Data Persistence and Program State

Data Persistence and Program State

Displaying a result is not the same as saving it. A value can be visible on screen during execution while still existing only in the temporary program workspace.

  • Programming

A Budget That Vanishes

Imagine a program that calculates a monthly budget. It stores income, expenses, and a remaining balance in variables, then displays the results on screen. While the program is running, everything appears to work correctly. But after you close the program or turn off the computer, where are those values?

The calculations, variables, lists, and other data created during execution exist in the program's active workspace: the CPU and main memory. This workspace is useful while the program runs, but it is volatile. If electrical power is lost, the data held there is erased. A later run of the program therefore has no memory of the earlier calculation unless the program saved the information somewhere persistent.

CPU and main memoryactive program workspaceElectrical powerrequiredSecondary memoryfiles and storage devicesStored dataremains without powerkeeps activeholds
Where does program data live while a program runs, and where can it remain after power is turned off?

State During Execution

Program state means the information that describes what a running program is currently doing and holding. During execution, the CPU evaluates conditions, calls functions, and loops through iterations. Main memory holds the data structures the program creates, including values stored in variables and lists. Together, the CPU and main memory provide the active workspace for the running program.

CPUevaluates and executesVariablestored valueProgram statecurrent executioninformationMain memoryholds program dataData structuresuch as a listupdates through executionholdsholdscontributes tocontributes to
What does a running program's current state contain, and where is that state held during execution?
Key point

Displaying a result is not the same as saving it. A value can be visible on screen during execution while still existing only in the temporary program workspace.

What Power Loss Changes

What do you think happens?

A budget program calculates a remaining balance and displays it, but does not write it to a file. What happens to that balance after the computer loses power?

  • It remains available automatically

  • It is erased from the CPU and main memory

  • It is converted automatically into a database file

Reveal answer
Answer:

It is erased from the CPU and main memory

The CPU and main memory are volatile. Their contents require electrical power, so program data held there disappears when power is lost.

Running programexecution in progressBudget variablesincome and expensesPower losselectrical power removedProgram stoppedexecution endedProgram dataerased from volatile memorypower is removedheld in active memorystopserases
What changes happen to the program and its data before and after power is suddenly removed?

Files Across Program Runs

Worked example

Saving a Monthly Budget

A budget program calculates income, expenses, and a remaining balance. How can the result be available during a later run?

  1. 1
    Calculate

    While the program runs, the CPU performs the calculations and main memory holds the variables containing the budget data.

  2. 2
    Write

    The program explicitly writes the budget information to a file in secondary memory instead of only displaying it.

  3. 3
    Stop

    The program can close, and the computer can lose power. The copy stored in secondary memory remains.

  4. 4
    Read

    During a later run, the program reads the file and brings the previously stored information back into its active workspace.

Result

The budget data persists because it was written to a file in non-volatile secondary memory.

Running programfirst runProgram datavariables and resultsFilesecondary memoryRunning programlater runProgram dataread back into memorycreateswritereadloads
How does data move from a running program into a file and back into memory during a later run?

A file is data stored in secondary memory. Writing data to a file moves a copy of the program's work from its fast, temporary workspace into persistent storage. On a later run, the program can read the file and use the stored information again. The program must perform these write and read operations explicitly; persistence does not happen merely because a value was displayed or held in a variable.

In the real world

A banking application must remember an account balance after a user logs out. A weather app may need to remember saved locations, and a game may need to save progress. These examples require data to be written to files in secondary memory so it can outlast the current execution.

Two File Categories

File categoryWhat it containsHow it is read
Text fileReadable plain text containing letters, numbers, and punctuationCan be opened directly in a text editor; programs can read and write it
Binary database fileStructured data in a special database formatRead and written by database software rather than directly in a text editor

Text files and binary database files both store persistent data, but they use different representations and access methods.

Text files are human-readable. Opening a budget CSV file, log file, or configuration file in a text editor shows the actual stored content as plain text. Binary database files are organized for specialized database software. They can contain many records arranged in tables, relationships between records, indexes for searching, and other internal structures. You normally work with the database software instead of opening the binary file directly in a text editor.

Text fileplain textText editorshows stored contentBinary databasefilestructured formatDatabase softwareintermediaryRecords and tablesorganized dataopened directly byread and written byorganizes
How are stored data and its organization different in a readable text file compared with a binary database file?

Mistakes About Persistence

  • Common mistake

    Assuming that displayed output is automatically saved

    What it looks like

    The budget result appears on screen, so the program will remember it next time.

    Why it fails

    Displaying a value does not write it to secondary memory. The value may exist only in volatile CPU and main memory.

    Do this instead

    Explicitly write the information to a file if it must persist beyond the current execution.

  • Common mistake

    Treating a variable as permanent storage

    What it looks like

    The balance is stored in a variable, so it will still exist after shutdown.

    Why it fails

    Variables created during execution are part of the program's active state in main memory.

    Do this instead

    Save a persistent copy in a file stored in secondary memory.

  • Common mistake

    Expecting a binary database file to be readable in a text editor

    What it looks like

    Opening a database file directly and expecting to see ordinary readable records.

    Why it fails

    Binary database files use structured formats intended for database software.

    Do this instead

    Use database software as the intermediary for reading and writing the database file.

  • Common mistake

    Confusing fast temporary memory with permanent storage

    What it looks like

    Assuming that the place where the program works is also the place where its data remains permanently.

    Why it fails

    CPU and main memory support active execution but are volatile, while secondary memory is non-volatile.

    Do this instead

    Keep the workspace and the persistent storage roles separate in your mental model.

Best practice

Whenever a program needs to remember something after it stops, identify the point where the data is written to a file. When the program starts again, identify the point where that file is read. This makes the boundary between temporary program state and persistent data explicit.

Check Your Model

EASY

A program creates a list of saved locations while it runs. It displays the list but never writes it to a file. Explain what happens to the list when the program ends, and describe what the program would need to do for the list to be available during a later run.

Hints
  • Ask where the list exists while the program is running.
  • Decide whether that location is volatile or non-volatile.
  • Identify the operation that moves data into persistent secondary memory.
Worked example

Reasoning About a Saved Game

A game stores progress in program variables and also writes that progress to a file. What is the role of each copy?

  1. 1
    During play

    The variables in the running program provide active state in CPU and main memory.

  2. 2
    When saving

    The program writes the progress to a file in secondary memory.

  3. 3
    After shutdown

    The active variables disappear when power is lost, but the file retains the saved progress.

  4. 4
    At the next run

    The program can read the file and restore the saved information into its active workspace.

Result

The variables support the current execution; the file provides persistence across executions.

The Persistence Boundary

  1. 1
    CPU and main memory are the active workspace for executing programs and holding their current data.
  2. 2
    That workspace is volatile because its contents depend on electrical power.
  3. 3
    Secondary memory, including files and storage devices, is non-volatile and retains written data without power.
  4. 4
    A program must explicitly write data to a file for it to persist across program runs.
  5. 5
    Text files store readable plain text, while binary database files use structured formats accessed through database software.

Key Takeaways

  • A running program's current state is held in the CPU and main memory, where execution and temporary data handling occur.
  • CPU and main memory are volatile, so their contents are erased when power is lost.
  • Files in non-volatile secondary memory allow data to survive program termination, shutdown, and later program runs.
  • Text files contain readable plain text, while binary database files contain structured formats intended for database software.

Related concepts

More concepts starting with D