Week 7 - Java Memory & GIT - Java Memory Management, JDK, JRE, JVM, Compilation, DevOps, Command Line and GIT intro Flashcards
A Java Development Kit (JDK) is used
used to create Java code
The Java code is then compiled into bytecode for a particular
Java Runtime Environment (JRE)
Since every machine can implement its own JREs and JVMs, Java source code does not have to be rewritten to be machine-specific. This is known as
Write Once, Run Anywhere (WORA)
describe the terms stack
The stack is a region of memory that is used for storing local variables, function arguments, and return addresses during the execution of a program. The stack is a last-in, first-out (LIFO) data structure, which means that the most recently added item is the first one to be removed. The stack is managed automatically by the program’s runtime system, and memory is automatically allocated and deallocated as needed. Because the stack is limited in size, it is typically used for storing data that is of fixed size and is short-lived.
Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects referred from the method that are in a heap.
Access to this memory is in Last-In-First-Out (LIFO) order. Whenever we call a new method, a new block is created on top of the stack which contains values specific to that method, like primitive variables and references to objects.
When the method finishes execution, its corresponding stack frame is flushed, the flow goes back to the calling method, and space becomes available for the next method.
Key Features of Stack Memory
It grows and shrinks as new methods are called and returned, respectively.
Variables inside the stack exist only as long as the method that created them is running.
It’s automatically allocated and deallocated when the method finishes execution.
If this memory is full, Java throws java.lang.StackOverFlowError.
Access to this memory is fast when compared to heap memory.
This memory is threadsafe, as each thread operates in its own stack
Describe Heap space in java
The heap, on the other hand, is a region of memory that is used for storing data that is allocated dynamically during the execution of a program. The heap is managed explicitly by the program, which means that the program is responsible for allocating and deallocating memory as needed. Data stored on the heap can be of variable size and can persist for the lifetime of the program. Because the heap is not limited in size, it is typically used for storing data that is of variable size and is long-lived. However, because the program is responsible for managing the heap, it is also more prone to memory leaks and other errors if not managed properly.
Introduction
Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory.
These objects have global access and we can access them from anywhere in the application.
We can break this memory model down into smaller parts, called generations, which are:
Young Generation – this is where all new objects are allocated and aged. A minor Garbage collection occurs when this fills up.
Old or Tenured Generation – this is where long surviving objects are stored. When objects are stored in the Young Generation, a threshold for the object’s age is set, and when that threshold is reached, the object is moved to the old generation.
Permanent Generation – this consists of JVM metadata for the runtime classes and application methods.
Key Features of Java Heap Memory
Some other features of heap space include:
It’s accessed via complex memory management techniques that include the Young Generation, Old or Tenured Generation, and Permanent Generation.
If heap space is full, Java throws java.lang.OutOfMemoryError.
Access to this memory is comparatively slower than stack memory
This memory, in contrast to stack, isn’t automatically deallocated. It needs Garbage Collector to free up unused objects so as to keep the efficiency of the memory usage.
Unlike stack, a heap isn’t threadsafe and needs to be guarded by properly synchronizing the code.
Access to stack memory can best be described as…?
LIFO; Last In First Out.
When a method is finished executing, which of the following does <b>not</b> happen with the stack?
The flow goes back to the calling method.
Its corresponding stack frame is flushed.
The memory allocated for the stack is lost until the machine is reset.
Space becomes available for the next method.
The memory allocated for the stack is lost until the machine is reset.
Which of the following is <b>not</b> true about heap space?
Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime.
New objects are always created in heap space.
References to objects in heap space are stored in stack memory.
Heap space uses static memory allocation.
Heap space uses static memory allocation.
Which is <b>not</b> a feature of heap memory?
If heap space is full, Java throws java.lang.OutOfMemoryError.
This memory, in contrast to stack, isn’t automatically deallocated.
Unlike stack, a heap isn’t threadsafe and needs to be guarded by properly synchronizing the code..
Access to this memory is faster compared to stack memory
Access to this memory is faster compared to stack memory.
Define and describe how garbage collection works.
Garbage collection is the process of removing objects from the heap which have no references to them.
Java abstracts the details away from the developer by allowing the JVM to handle memory management itself.
Garbage collection is run in the background by the JVM. There is no way we can explicitly force garbage collection to happen, but we can request garbage collection to be run through the use of one of the following:
System.gc()
Runtime.getRuntime().gc()
System.runFinalize()
Garbage collection can be forced by
It can never be forced.
Variable references are stored in the Stack. T/F?
True
What is the easiest way to immediately force Java garbage collection?
You cannot force Java garbage collection
A Java programming language developer must write memory management into their programs. T/F?
False
Which is NOT a consideration when selecting an operating system to use for development?
Marketing of the final product
Speed
Platforms for the final product (local machine, cloud, etc)
Learning curve
Marketing of the final product
gitbash: ls
This command lists directory contents.
It lists files and directories.
Some versions may support color-coding. The names in blue represent the names of directories.
$ ls -l | more
This command helps paginate the output so you can view page by page. Otherwise the listing scrolls down rapidly. You can always use ctrl + c to return to the command line.
Note: more is not supported in GitBash for Windows.
$ ls –I
This command shows more details of the contents in the directory. It lists the following:
Permissions associated with the fileThe owner of the file
The group associated with the file
The size of the file
The timestamp
The name of the file
cd
This changes the current directory. Note that it uses a forward slash.
$ cd /var/log
pwd
One way to identify the directory you are working in is the pwd command. It displays the current working directory path and is useful when directory changes are made frequently.
$ pwd
$ mkdir myproject
The mkdir command makes a directory. The command is written as follows: mkdir [directory name]
$ cat > newfilename
The cat command can be used to create, view, and concatenate files. The example below creates a new file called “newfilename”.
$ cat sourcefilename > destinationfilename
Another way to create a file using cat:
The cat example below will copy the file “sourcefilename” into the file “destinationfilename”.
$ touch filename
The touch command creates an empty file for editing later. The command below creates an empty file called “filename”. From there you can use a terminal-based editor like “vi” to edit the file.
$ echo “Some line” > file1.txt
The echo command prints the strings that are passed as arguments to the standard output, which can be redirected to a file. To create a new file run the echo command followed by the text you want to print and use the redirection operator > (as explained with cat above) to write the output to the file you want to create.
The command below will create the file “file.txt” containing the text “Some line”.
$ grep Aaron filename
The grep command can be used to search files and directories (and subdirectories) for a string. The example below searches the file “filename” for the string “Aaron”.
$ diff file1.txt file2.txt
The diffcommand compares two files line by line to find differences. The output will be the lines that are different.
mv
The mv command moves a file or renames it. Some examples; inline comments denoted with //
$ mv file1 directory1
moves ‘file1’ to ‘directory1’