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.
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.
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.
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?
Reveal 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.
Files Across Program Runs
Saving a Monthly Budget
A budget program calculates income, expenses, and a remaining balance. How can the result be available during a later run?
- 1Calculate
While the program runs, the CPU performs the calculations and main memory holds the variables containing the budget data.
- 2Write
The program explicitly writes the budget information to a file in secondary memory instead of only displaying it.
- 3Stop
The program can close, and the computer can lose power. The copy stored in secondary memory remains.
- 4Read
During a later run, the program reads the file and brings the previously stored information back into its active workspace.
The budget data persists because it was written to a file in non-volatile secondary memory.
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.
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 category | What it contains | How it is read |
|---|---|---|
| Text file | Readable plain text containing letters, numbers, and punctuation | Can be opened directly in a text editor; programs can read and write it |
| Binary database file | Structured data in a special database format | Read 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.
Mistakes About Persistence
Common mistake Assuming that displayed output is automatically saved
What it looks likeThe budget result appears on screen, so the program will remember it next time.
Why it failsDisplaying a value does not write it to secondary memory. The value may exist only in volatile CPU and main memory.
Do this insteadExplicitly 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 likeThe balance is stored in a variable, so it will still exist after shutdown.
Why it failsVariables created during execution are part of the program's active state in main memory.
Do this insteadSave 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 likeOpening a database file directly and expecting to see ordinary readable records.
Why it failsBinary database files use structured formats intended for database software.
Do this insteadUse database software as the intermediary for reading and writing the database file.
Common mistake Confusing fast temporary memory with permanent storage
What it looks likeAssuming that the place where the program works is also the place where its data remains permanently.
Why it failsCPU and main memory support active execution but are volatile, while secondary memory is non-volatile.
Do this insteadKeep the workspace and the persistent storage roles separate in your mental model.
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
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.
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?
- 1During play
The variables in the running program provide active state in CPU and main memory.
- 2When saving
The program writes the progress to a file in secondary memory.
- 3After shutdown
The active variables disappear when power is lost, but the file retains the saved progress.
- 4At the next run
The program can read the file and restore the saved information into its active workspace.
The variables support the current execution; the file provides persistence across executions.
The Persistence Boundary
- 1CPU and main memory are the active workspace for executing programs and holding their current data.
- 2That workspace is volatile because its contents depend on electrical power.
- 3Secondary memory, including files and storage devices, is non-volatile and retains written data without power.
- 4A program must explicitly write data to a file for it to persist across program runs.
- 5Text 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.