Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the great charms of the Java programing language is that even though it's a compiled language, it manages memory for you, unlike languages like C, C++, Pascal, and others. You as the programmer don't have to explicitly allocate and de-allocate memory whenever you create an object. The Garbage Collector is a major feature of the Java Virtual Machine. I am going to describe how the Garbage Collector works and how little attention you as the programmer need to give it. When you create a variable that references a complex object, the object itself is stored in an area of memory called heap memory.
There are two types of memory; stack and heap. The stack memory is somewhat faster; heap a little bit slower, but heap is more dynamic. When you create primitive variables, depending on their context, they might be stored in stack or heap, but complex objects are always stored in heap. As long as any variable references an object in memory, that object will be retained, it won't be eligible for garbage collection. So if you create a variable and it points to a string, a date, an array, a collection, or any other complex object, as long as that variable can be addressed in your code, the object will be available.
When all references to the object expire, that is when you can't get to the object through your code anymore, then that object is eligible for garbage collection. So when do references expire? There are two models that you can look at. First of all, when you declare a variable that's local to a function or to a code block, as soon as the function or code block is done executing, that variable is no longer available to you, and so it has expired and is eligible for garbage collection. Within this function there are two scopes.
Variables declared directly within the function are available as long as the function is executing. So the variable in function would be available until the function is done, and then it would expire. I've spoken previously about the fact that in Java, scope is also defined by code blocks. So in the second part of the code, in the for loop, I am declaring an integer as a part of the for loop, the name of the variable as i, and as soon as that for loop is done executing, that variable is no longer in scope and it's eligible for garbage collection.
For variables that are more persistent, such as fields within objects, you can explicitly expire them by setting their value to null, N-U-L-L. So for example, here is string named expireMe, and if I want to make it eligible for garbage collection, I simply set its value to null. The object in memory can't be reached in my code anymore and therefore it's eligible for garbage collection. So when does garbage collection happen? The Garbage Collector runs in its own thread, so it doesn't affect your applications.
As long as the system on which your application is running has adequate resources, you shouldn't notice that the Collector is running. And the Garbage Collector has incredible autonomy. The Java Virtual Machine runs the Garbage Collector when it thinks it needs to, and when the Garbage Collector identifies an object that no longer has any references, it has the option of either destroying it and reclaiming the memory or not. It has its own algorithm, its own logic, for figuring out how to manage the memory, and it's not up to you.
You as a programmer cannot force garbage collection in Java. There are methods available in the language, such as System.gc() and Runtime.gc(), gc stands for Garbage Collector. These are methods that send messages to the JVM, that say it might be a good idea to collect garbage right now, but there is no guarantee that it will happen. And in fact, many Java developers recommend not using these functions. It can mislead you into thinking that you are getting all your memory back right away, but that's not necessarily what's going to happen.
If you're running a complex application and you are challenging the available memory on the system, you will know, because the Java Virtual Machine will throw an error called OutOfMemoryError, and you will see these errors happen in more complex applications where an application is running on low memory devices or systems. You can manage the memory of the Java Virtual Machine by setting certain parameters when you start up an application. You can also tune your application by minimizing the number of objects that you create.
As you've seen in many of the examples in this course, there are different ways of architecting Java code, so that you can create a whole bunch of objects, or you can reuse objects that you've already created. In general, it's better to reuse. If you're concerned about memory usage, you can use these methods to find out how much memory is available: Runtime.maxMemory(), Runtime. totalMemory(), and Runtime.freeMemory(). Take a look at the Java docs for these methods to find out what information they are giving you. And you can use command line options to manage the amount of available heap memory.
You can manage the memory in three areas; in the initial heap size, in the maximum heap size, and in the heap size for what are called young generation objects; objects that have just been created. If you want more information about memory management, take a look at the documentation for the Java programing language on the Oracle website. You will find that there is an enormous amount of information available. But when you're writing your first simple Java applications just know that the Garbage Collector is highly automated and as long as you are not running out of memory, you might not need to worry about it.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98192 Viewers
61 Video lessons · 85490 Viewers
71 Video lessons · 69457 Viewers
56 Video lessons · 101801 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.