Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Advanced Topics : Memory

Let’s begin by understanding what memory is in the context of Java. You can think of each program you write as being allowed to use some amount of memory. One program can’t be given all of the memory available in a computer, since that would mean that other programs wouldn’t be able to have enough memory because one of them was using it all. While memory works a bit different than the following illustration, let’s assume that all programs have been allocated a fixed amount to begin with and they can’t ever have more.

A program’s memory is divided into it’s stack and heap. When you first start the program, any variables defined when you compile the program will be placed in the heap.

While the program runs, any variables that are created from things like loops and recursion will be placed on the stack. Because we don’t know how many times a loop might run, and hence how many variables we will be allocated, the stack is flexible. In this illustration the program’s stack will “grow” as we allocate more memory, eventually hitting a limit and returning a stack overflow error:

Exception in thread "main" java.lang.StackOverflowError

or a memory error:

Exception in thread "main" java.lang.OutOfMemoryError

Side bar: The Java Virtual Machine
It is difficult to talk about Java and memory without mentioning the Java virtual machine. In computing, the computer hardware plus the operating system are referred to as, the machine. Programs written in most languages run natively, that is, they are executed by the operating system running on that hardware. Java is different. Java applications do not run natively. There is a layer of software between your Java application and the underlying operating system. This layer is the virtual machine. Java source code is compiled into bytecode (not native code). The bytecode is run (managed) by the virtual machine which interacts with the underlying operating system. The rationale for this is the “write once, run anywhere” idea. The Java virtual machine that runs on macOS is identical (in theory) to the Java virtual machines that run on Windows, Linux, SparcOS, and other operating systems. As a result, a developer (like you) can create a Java application that targets the Java virtual machine, and that application will be able to run on many different operating systems. This is a significant benefit, relieving the developer from having to create multiple versions of the application. Cool.

Variables in Memory

Lab 1X – Queen’s Attack

This lab tackles the memory problems talked about above. There is a potential for running out of memory because the number of combinations to check for are quite large, leading you to possibly create too many variables. Even though you might hear from time to time that modern systems don’t have to worry about memory as much as older systems, there is still a real possibility of any program to overflow its stack.