Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Automatic memory management === Java uses an [[Garbage collection (computer science)|automatic garbage collector]] to manage memory in the [[object lifetime|object lifecycle]]. The programmer determines when objects are created, and the Java runtime is responsible for recovering the memory once objects are no longer in use. Once no references to an object remain, the [[unreachable memory]] becomes eligible to be freed automatically by the garbage collector. Something similar to a [[memory leak]] may still occur if a programmer's code holds a reference to an object that is no longer needed, typically when objects that are no longer needed are stored in containers that are still in use.{{sfn|Bloch|2018|loc=§Item 7: Eliminate obsolete object references|p=26-28}} If methods for a non-existent object are called, a [[null pointer]] exception is thrown.<ref>{{cite web |url=http://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html |title=NullPointerException |publisher=Oracle |access-date=2014-05-06 |archive-url=https://web.archive.org/web/20140506214735/http://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html |archive-date=May 6, 2014 |url-status=live}}</ref><ref>{{cite web |url=http://www.artima.com/designtechniques/exceptions.html |title=Exceptions in Java |publisher=Artima.com |access-date=2010-08-10 |archive-url=https://web.archive.org/web/20090121152332/http://www.artima.com/designtechniques/exceptions.html |archive-date=January 21, 2009 |url-status=live}}</ref> One of the ideas behind Java's automatic memory management model is that programmers can be spared the burden of having to perform manual memory management. In some languages, memory for the creation of objects is implicitly allocated on the [[Stack (abstract data type)|stack]] or explicitly allocated and deallocated from the [[Memory management#DYNAMIC|heap]]. In the latter case, the responsibility of managing memory resides with the programmer. If the program does not deallocate an object, a [[memory leak]] occurs.{{sfn|Bloch|2018|loc=§Item 7: Eliminate obsolete object references|p=26-28}} If the program attempts to access or deallocate memory that has already been deallocated, the result is undefined and difficult to predict, and the program is likely to become unstable or crash. This can be partially remedied by the use of [[smart pointer]]s, but these add overhead and complexity. Garbage collection does not prevent [[logical address|logical memory]] leaks, i.e. those where the memory is still referenced but never used.{{sfn|Bloch|2018|loc=§Item 7: Eliminate obsolete object references|p=26-28}} Garbage collection may happen at any time. Ideally, it will occur when a program is idle. It is guaranteed to be triggered if there is insufficient free memory on the heap to allocate a new object; this can cause a program to stall momentarily. Explicit memory management is not possible in Java. Java does not support C/C++ style [[pointer (computer programming)|pointer arithmetic]], where object addresses can be arithmetically manipulated (e.g. by adding or subtracting an offset). This allows the garbage collector to relocate referenced objects and ensures type safety and security. As in C++ and some other object-oriented languages, variables of Java's [[primitive data type]]s are either stored directly in fields (for objects) or on the [[Stack-based memory allocation|stack]] (for methods) rather than on the heap, as is commonly true for non-primitive data types (but see [[escape analysis]]). This was a conscious decision by Java's designers for performance reasons. Java contains multiple types of garbage collectors. Since Java 9, HotSpot uses the [[Garbage-first collector|Garbage First Garbage Collector]] (G1GC) as the default.<ref>{{cite web |url=https://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html |title=Java HotSpot™ Virtual Machine Performance Enhancements |publisher=Oracle.com |access-date=2017-04-26 |archive-url=https://web.archive.org/web/20170529071720/http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html |archive-date=May 29, 2017 |url-status=live}}</ref> However, there are also several other garbage collectors that can be used to manage the heap, such as the Z Garbage Collector (ZGC) introduced in Java 11, and Shenandoah GC, introduced in Java 12 but unavailable in Oracle-produced OpenJDK builds. Shenandoah is instead available in third-party builds of OpenJDK, such as [[Adoptium#Eclipse Temurin|Eclipse Temurin]]. For most applications in Java, G1GC is sufficient. In prior versions of Java, such as Java 8, the [https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html Parallel Garbage Collector] was used as the default garbage collector. Having solved the memory management problem does not relieve the programmer of the burden of handling properly other kinds of resources, like network or database connections, file handles, etc., especially in the presence of exceptions.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)