-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathjava-memory-management.html
More file actions
181 lines (160 loc) · 55.8 KB
/
java-memory-management.html
File metadata and controls
181 lines (160 loc) · 55.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Java - Java Memory Management</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Setting the Heap, PermGen and Stack sizes, Garbage collection, Memory leaks in Java, Finalization, Manually triggering GC">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="Java - Java Memory Management">
<meta property="og:description" content="Setting the Heap, PermGen and Stack sizes, Garbage collection, Memory leaks in Java, Finalization, Manually triggering GC">
<meta property="og:type" content="article">
<meta property="og:url" content="/java/java-memory-management.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="Java - Java Memory Management">
<meta name="twitter:description" content="Setting the Heap, PermGen and Stack sizes, Garbage collection, Memory leaks in Java, Finalization, Manually triggering GC">
<meta name="twitter:url" content="/java/java-memory-management.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/1591.975d1834.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Java</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/java/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/java/java-editions-versions-releases-and-distributions.html" class="sidebar-link">Java Editions, Versions, Releases and Distributions</a></li><li><a href="/java/installing-java-standard-edition.html" class="sidebar-link">Installing Java (Standard Edition)</a></li><li><a href="/java/getting-started-with-java-language.html" class="sidebar-link">Getting started with Java Language</a></li><li><a href="/java/type-conversion.html" class="sidebar-link">Type Conversion</a></li><li><a href="/java/getters-and-setters.html" class="sidebar-link">Getters and Setters</a></li><li><a href="/java/reference-data-types.html" class="sidebar-link">Reference Data Types</a></li><li><a href="/java/java-compiler-javac.html" class="sidebar-link">Java Compiler - 'javac'</a></li><li><a href="/java/documenting-java-code.html" class="sidebar-link">Documenting Java Code</a></li><li><a href="/java/command-line-argument-processing.html" class="sidebar-link">Command line Argument Processing</a></li><li><a href="/java/the-java-command-java-and-javaw.html" class="sidebar-link">The Java Command - 'java' and 'javaw'</a></li><li><a href="/java/literals.html" class="sidebar-link">Literals</a></li><li><a href="/java/primitive-data-types.html" class="sidebar-link">Primitive Data Types</a></li><li><a href="/java/strings.html" class="sidebar-link">Strings</a></li><li><a href="/java/stringbuffer.html" class="sidebar-link">StringBuffer</a></li><li><a href="/java/stringbuilder.html" class="sidebar-link">StringBuilder</a></li><li><a href="/java/string-tokenizer.html" class="sidebar-link">String Tokenizer</a></li><li><a href="/java/splitting-a-string-into-fixed-length-parts.html" class="sidebar-link">Splitting a string into fixed length parts</a></li><li><a href="/java/date-class.html" class="sidebar-link">Date Class</a></li><li><a href="/java/dates-and-time-java-time.html" class="sidebar-link">Dates and Time (java.time.*)</a></li><li><a href="/java/localtime.html" class="sidebar-link">LocalTime</a></li><li><a href="/java/bigdecimal.html" class="sidebar-link">BigDecimal</a></li><li><a href="/java/biginteger.html" class="sidebar-link">BigInteger</a></li><li><a href="/java/numberformat.html" class="sidebar-link">NumberFormat</a></li><li><a href="/java/bit-manipulation.html" class="sidebar-link">Bit Manipulation</a></li><li><a href="/java/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/java/collections.html" class="sidebar-link">Collections</a></li><li><a href="/java/lists.html" class="sidebar-link">Lists</a></li><li><a href="/java/sets.html" class="sidebar-link">Sets</a></li><li><a href="/java/list-vs-set.html" class="sidebar-link">List vs SET</a></li><li><a href="/java/maps.html" class="sidebar-link">Maps</a></li><li><a href="/java/linkedhashmap.html" class="sidebar-link">LinkedHashMap</a></li><li><a href="/java/weakhashmap.html" class="sidebar-link">WeakHashMap</a></li><li><a href="/java/sortedmap.html" class="sidebar-link">SortedMap</a></li><li><a href="/java/treemap-and-treeset.html" class="sidebar-link">TreeMap and TreeSet</a></li><li><a href="/java/queues-and-deques.html" class="sidebar-link">Queues and Deques</a></li><li><a href="/java/dequeue-interface.html" class="sidebar-link">Dequeue Interface</a></li><li><a href="/java/enums.html" class="sidebar-link">Enums</a></li><li><a href="/java/enum-map.html" class="sidebar-link">Enum Map</a></li><li><a href="/java/enumset-class.html" class="sidebar-link">EnumSet class</a></li><li><a href="/java/enum-starting-with-number.html" class="sidebar-link">Enum starting with number</a></li><li><a href="/java/hashtable.html" class="sidebar-link">Hashtable</a></li><li><a href="/java/operators.html" class="sidebar-link">Operators</a></li><li><a href="/java/constructors.html" class="sidebar-link">Constructors</a></li><li><a href="/java/object-class-methods-and-constructor.html" class="sidebar-link">Object Class Methods and Constructor</a></li><li><a href="/java/annotations.html" class="sidebar-link">Annotations</a></li><li><a href="/java/immutable-class.html" class="sidebar-link">Immutable Class</a></li><li><a href="/java/immutable-objects.html" class="sidebar-link">Immutable Objects</a></li><li><a href="/java/visibility-controlling-access-to-members-of-a-class.html" class="sidebar-link">Visibility (controlling access to members of a class)</a></li><li><a href="/java/generics.html" class="sidebar-link">Generics</a></li><li><a href="/java/classes-and-objects.html" class="sidebar-link">Classes and Objects</a></li><li><a href="/java/local-inner-class.html" class="sidebar-link">Local Inner Class</a></li><li><a href="/java/nested-and-inner-classes.html" class="sidebar-link">Nested and Inner Classes</a></li><li><a href="/java/the-java-util-objects-class.html" class="sidebar-link">The java.util.Objects Class</a></li><li><a href="/java/default-methods.html" class="sidebar-link">Default Methods</a></li><li><a href="/java/packages.html" class="sidebar-link">Packages</a></li><li><a href="/java/inheritance.html" class="sidebar-link">Inheritance</a></li><li><a href="/java/reference-types.html" class="sidebar-link">Reference Types</a></li><li><a href="/java/console-i-o.html" class="sidebar-link">Console I/O</a></li><li><a href="/java/streams.html" class="sidebar-link">Streams</a></li><li><a href="/java/inputstreams-and-outputstreams.html" class="sidebar-link">InputStreams and OutputStreams</a></li><li><a href="/java/readers-and-writers.html" class="sidebar-link">Readers and Writers</a></li><li><a href="/java/preferences.html" class="sidebar-link">Preferences</a></li><li><a href="/java/collection-factory-methods.html" class="sidebar-link">Collection Factory Methods</a></li><li><a href="/java/alternative-collections.html" class="sidebar-link">Alternative Collections</a></li><li><a href="/java/concurrent-collections.html" class="sidebar-link">Concurrent Collections</a></li><li><a href="/java/choosing-collections.html" class="sidebar-link">Choosing Collections</a></li><li><a href="/java/super-keyword.html" class="sidebar-link">super keyword</a></li><li><a href="/java/serialization.html" class="sidebar-link">Serialization</a></li><li><a href="/java/optional.html" class="sidebar-link">Optional</a></li><li><a href="/java/object-references.html" class="sidebar-link">Object References</a></li><li><a href="/java/exceptions-and-exception-handling.html" class="sidebar-link">Exceptions and exception handling</a></li><li><a href="/java/calendar-and-its-subclasses.html" class="sidebar-link">Calendar and its Subclasses</a></li><li><a href="/java/using-the-static-keyword.html" class="sidebar-link">Using the static keyword</a></li><li><a href="/java/properties-class.html" class="sidebar-link">Properties Class</a></li><li><a href="/java/lambda-expressions.html" class="sidebar-link">Lambda Expressions</a></li><li><a href="/java/basic-control-structures.html" class="sidebar-link">Basic Control Structures</a></li><li><a href="/java/bufferedwriter.html" class="sidebar-link">BufferedWriter</a></li><li><a href="/java/new-file-i-o.html" class="sidebar-link">New File I/O</a></li><li><a href="/java/file-i-o.html" class="sidebar-link">File I/O</a></li><li><a href="/java/scanner.html" class="sidebar-link">Scanner</a></li><li><a href="/java/interfaces.html" class="sidebar-link">Interfaces</a></li><li><a href="/java/regular-expressions.html" class="sidebar-link">Regular Expressions</a></li><li><a href="/java/comparable-and-comparator.html" class="sidebar-link">Comparable and Comparator</a></li><li><a href="/java/java-floating-point-operations.html" class="sidebar-link">Java Floating Point Operations</a></li><li><a href="/java/currency-and-money.html" class="sidebar-link">Currency and Money</a></li><li><a href="/java/object-cloning.html" class="sidebar-link">Object Cloning</a></li><li><a href="/java/recursion.html" class="sidebar-link">Recursion</a></li><li><a href="/java/converting-to-and-from-strings.html" class="sidebar-link">Converting to and from Strings</a></li><li><a href="/java/random-number-generation.html" class="sidebar-link">Random Number Generation</a></li><li><a href="/java/singletons.html" class="sidebar-link">Singletons</a></li><li><a href="/java/autoboxing.html" class="sidebar-link">Autoboxing</a></li><li><a href="/java/2d-graphics-in-java.html" class="sidebar-link">2D Graphics in Java</a></li><li><a href="/java/jaxb.html" class="sidebar-link">JAXB</a></li><li><a href="/java/class-java-reflection.html" class="sidebar-link">Class - Java Reflection</a></li><li><a href="/java/networking.html" class="sidebar-link">Networking</a></li><li><a href="/java/nio-networking.html" class="sidebar-link">NIO - Networking</a></li><li><a href="/java/httpurlconnection.html" class="sidebar-link">HttpURLConnection</a></li><li><a href="/java/jax-ws.html" class="sidebar-link">JAX-WS</a></li><li><a href="/java/nashorn-javascript-engine.html" class="sidebar-link">Nashorn JavaScript engine</a></li><li><a href="/java/java-native-interface.html" class="sidebar-link">Java Native Interface</a></li><li><a href="/java/functional-interfaces.html" class="sidebar-link">Functional Interfaces</a></li><li><a href="/java/fluent-interface.html" class="sidebar-link">Fluent Interface</a></li><li><a href="/java/remote-method-invocation-rmi.html" class="sidebar-link">Remote Method Invocation (RMI)</a></li><li><a href="/java/iterator-and-iterable.html" class="sidebar-link">Iterator and Iterable</a></li><li><a href="/java/reflection-api.html" class="sidebar-link">Reflection API</a></li><li><a href="/java/bytebuffer.html" class="sidebar-link">ByteBuffer</a></li><li><a href="/java/applets.html" class="sidebar-link">Applets</a></li><li><a href="/java/expressions.html" class="sidebar-link">Expressions</a></li><li><a href="/java/json-in-java.html" class="sidebar-link">JSON in Java</a></li><li><a href="/java/xml-parsing-using-the-jaxp-apis.html" class="sidebar-link">XML Parsing using the JAXP APIs</a></li><li><a href="/java/xml-xpath-evaluation.html" class="sidebar-link">XML XPath Evaluation</a></li><li><a href="/java/xom-xml-object-model.html" class="sidebar-link">XOM - XML Object Model</a></li><li><a href="/java/polymorphism.html" class="sidebar-link">Polymorphism</a></li><li><a href="/java/encapsulation.html" class="sidebar-link">Encapsulation</a></li><li><a href="/java/java-agents.html" class="sidebar-link">Java Agents</a></li><li><a href="/java/varargs-variable-argument.html" class="sidebar-link">Varargs (Variable Argument)</a></li><li><a href="/java/logging-java-util-logging.html" class="sidebar-link">Logging (java.util.logging)</a></li><li><a href="/java/log4j-log4j2.html" class="sidebar-link">log4j / log4j2</a></li><li><a href="/java/oracle-official-code-standard.html" class="sidebar-link">Oracle Official Code Standard</a></li><li><a href="/java/character-encoding.html" class="sidebar-link">Character encoding</a></li><li><a href="/java/apache-commons-lang.html" class="sidebar-link">Apache Commons Lang</a></li><li><a href="/java/localization-and-internationalization.html" class="sidebar-link">Localization and Internationalization</a></li><li><a href="/java/parallel-programming-with-fork-join-framework.html" class="sidebar-link">Parallel programming with Fork/Join framework</a></li><li><a href="/java/non-access-modifiers.html" class="sidebar-link">Non-Access Modifiers</a></li><li><a href="/java/process.html" class="sidebar-link">Process</a></li><li><a href="/java/java-native-access.html" class="sidebar-link">Java Native Access</a></li><li><a href="/java/modules.html" class="sidebar-link">Modules</a></li><li><a href="/java/concurrent-programming-threads.html" class="sidebar-link">Concurrent Programming (Threads)</a></li><li><a href="/java/executor-executorservice-and-thread-pools.html" class="sidebar-link">Executor, ExecutorService and Thread pools</a></li><li><a href="/java/threadlocal.html" class="sidebar-link">ThreadLocal</a></li><li><a href="/java/using-threadpoolexecutor-in-multithreaded-applications.html" class="sidebar-link">Using ThreadPoolExecutor in MultiThreaded applications.</a></li><li><a href="/java/common-java-pitfalls.html" class="sidebar-link">Common Java Pitfalls</a></li><li><a href="/java/java-pitfalls-exception-usage.html" class="sidebar-link">Java Pitfalls - Exception usage</a></li><li><a href="/java/java-pitfalls-language-syntax.html" class="sidebar-link">Java Pitfalls - Language syntax</a></li><li><a href="/java/java-pitfalls-threads-and-concurrency.html" class="sidebar-link">Java Pitfalls - Threads and Concurrency</a></li><li><a href="/java/java-pitfalls-nulls-and-nullpointerexception.html" class="sidebar-link">Java Pitfalls - Nulls and NullPointerException</a></li><li><a href="/java/java-pitfalls-performance-issues.html" class="sidebar-link">Java Pitfalls - Performance Issues</a></li><li><a href="/java/serviceloader.html" class="sidebar-link">ServiceLoader</a></li><li><a href="/java/classloaders.html" class="sidebar-link">Classloaders</a></li><li><a href="/java/creating-images-programmatically.html" class="sidebar-link">Creating Images Programmatically</a></li><li><a href="/java/atomic-types.html" class="sidebar-link">Atomic Types</a></li><li><a href="/java/rsa-encryption.html" class="sidebar-link">RSA Encryption</a></li><li><a href="/java/secure-objects.html" class="sidebar-link">Secure objects</a></li><li><a href="/java/security-cryptography.html" class="sidebar-link">Security & Cryptography</a></li><li><a href="/java/securitymanager.html" class="sidebar-link">SecurityManager</a></li><li><a href="/java/jndi.html" class="sidebar-link">JNDI</a></li><li><a href="/java/sun-misc-unsafe.html" class="sidebar-link">sun.misc.Unsafe</a></li><li><a href="/java/java-memory-model.html" class="sidebar-link">Java Memory Model</a></li><li><a href="/java/java-deployment.html" class="sidebar-link">Java deployment</a></li><li><a href="/java/java-plugin-system-implementations.html" class="sidebar-link">Java plugin system implementations</a></li><li><a href="/java/javabean.html" class="sidebar-link">JavaBean</a></li><li><a href="/java/java-se-7-features.html" class="sidebar-link">Java SE 7 Features</a></li><li><a href="/java/java-se-8-features.html" class="sidebar-link">Java SE 8 Features</a></li><li><a href="/java/dynamic-method-dispatch.html" class="sidebar-link">Dynamic Method Dispatch</a></li><li><a href="/java/generating-java-code.html" class="sidebar-link">Generating Java Code</a></li><li><a href="/java/jshell.html" class="sidebar-link">JShell</a></li><li><a href="/java/stack-walking-api.html" class="sidebar-link">Stack-Walking API</a></li><li><a href="/java/sockets.html" class="sidebar-link">Sockets</a></li><li><a href="/java/java-sockets.html" class="sidebar-link">Java Sockets</a></li><li><a href="/java/ftp-file-transfer-protocol.html" class="sidebar-link">FTP (File Transfer Protocol)</a></li><li><a href="/java/using-other-scripting-languages-in-java.html" class="sidebar-link">Using Other Scripting Languages in Java</a></li><li><a href="/java/c-comparison.html" class="sidebar-link">C++ Comparison</a></li><li><a href="/java/audio.html" class="sidebar-link">Audio</a></li><li><a href="/java/java-print-service.html" class="sidebar-link">Java Print Service</a></li><li><a href="/java/completablefuture.html" class="sidebar-link">CompletableFuture</a></li><li><a href="/java/runtime-commands.html" class="sidebar-link">Runtime Commands</a></li><li><a href="/java/unit-testing.html" class="sidebar-link">Unit Testing</a></li><li><a href="/java/asserting.html" class="sidebar-link">Asserting</a></li><li><a href="/java/multi-release-jar-files.html" class="sidebar-link">Multi-Release JAR Files</a></li><li><a href="/java/just-in-time-jit-compiler.html" class="sidebar-link">Just in Time (JIT) compiler</a></li><li><a href="/java/bytecode-modification.html" class="sidebar-link">Bytecode Modification</a></li><li><a href="/java/disassembling-and-decompiling.html" class="sidebar-link">Disassembling and Decompiling</a></li><li><a href="/java/jmx.html" class="sidebar-link">JMX</a></li><li><a href="/java/java-virtual-machine-jvm.html" class="sidebar-link">Java Virtual Machine (JVM)</a></li><li><a href="/java/xjc.html" class="sidebar-link">XJC</a></li><li><a href="/java/jvm-flags.html" class="sidebar-link">JVM Flags</a></li><li><a href="/java/jvm-tool-interface.html" class="sidebar-link">JVM Tool Interface</a></li><li><a href="/java/java-memory-management.html" aria-current="page" class="active sidebar-link">Java Memory Management</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/java/java-memory-management.html#setting-the-heap-permgen-and-stack-sizes" class="sidebar-link">Setting the Heap, PermGen and Stack sizes</a></li><li class="sidebar-sub-header"><a href="/java/java-memory-management.html#garbage-collection" class="sidebar-link">Garbage collection</a></li><li class="sidebar-sub-header"><a href="/java/java-memory-management.html#memory-leaks-in-java" class="sidebar-link">Memory leaks in Java</a></li><li class="sidebar-sub-header"><a href="/java/java-memory-management.html#finalization" class="sidebar-link">Finalization</a></li><li class="sidebar-sub-header"><a href="/java/java-memory-management.html#manually-triggering-gc" class="sidebar-link">Manually triggering GC</a></li></ul></li><li><a href="/java/java-performance-tuning.html" class="sidebar-link">Java Performance Tuning</a></li><li><a href="/java/benchmarks.html" class="sidebar-link">Benchmarks</a></li><li><a href="/java/fileupload-to-aws.html" class="sidebar-link">FileUpload to AWS</a></li><li><a href="/java/appdynamics-and-tibco-businessworks-instrumentation-for-easy-integration.html" class="sidebar-link">AppDynamics and TIBCO BusinessWorks Instrumentation for Easy Integration</a></li><li><a href="/java/the-classpath.html" class="sidebar-link">The Classpath</a></li><li><a href="/java/resources-on-classpath.html" class="sidebar-link">Resources (on classpath)</a></li><li><a href="/java/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="java-memory-management"><a href="#java-memory-management" class="header-anchor">#</a> Java Memory Management</h1> <h2 id="setting-the-heap-permgen-and-stack-sizes"><a href="#setting-the-heap-permgen-and-stack-sizes" class="header-anchor">#</a> Setting the Heap, PermGen and Stack sizes</h2> <p>When a Java virtual machine starts, it needs to know how big to make the Heap, and the default size for thread stacks. These can be specified using command-line options on the <code>java</code> command. For versions of Java prior to Java 8, you can also specify the size of the PermGen region of the Heap.</p> <p>Note that PermGen was removed in Java 8, and if you attempt to set the PermGen size the option will be ignored (with a warning message).</p> <p>If you don't specify Heap and Stack sizes explicitly, the JVM will use defaults that are calculated in a version and platform specific way. This may result in your application using too little or too much memory. This is typically OK for thread stacks, but it can be problematic for a program that uses a lot of memory.</p> <p><strong>Setting the Heap, PermGen and default Stack sizes:</strong></p> <p>The following JVM options set the heap size:</p> <ul><li><code>-Xms<size></code> - sets the initial heap size</li> <li><code>-Xmx<size></code> - sets the maximum heap size</li> <li><code>-XX:PermSize<size></code> - sets the initial PermGen size</li> <li><code>-XX:MaxPermSize<size></code> - sets the maximum PermGen size</li> <li><code>-Xss<size></code> - sets the default thread stack size</li></ul> <p>The <code><size></code> parameter can be a number of bytes, or can have a suffix of <code>k</code>, <code>m</code> or <code>g</code>. The latter specify the size in kilobytes, megabytes and gigabytes respectively.</p> <p>Examples:</p> <div class="language-java extra-class"><pre class="language-java"><code>$ java <span class="token operator">-</span><span class="token class-name">Xms512m</span> <span class="token operator">-</span><span class="token class-name">Xmx1024m</span> <span class="token class-name">JavaApp</span>
$ java <span class="token operator">-</span>XX<span class="token operator">:</span><span class="token class-name">PermSize</span><span class="token operator">=</span><span class="token number">64</span>m <span class="token operator">-</span>XX<span class="token operator">:</span><span class="token class-name">MaxPermSize</span><span class="token operator">=</span><span class="token number">128</span>m <span class="token class-name">JavaApp</span>
$ java <span class="token operator">-</span><span class="token class-name">Xss512k</span> <span class="token class-name">JavaApp</span>
</code></pre></div><p><strong>Finding the default sizes:</strong></p> <p>The <code>-XX:+printFlagsFinal</code> option can be used to print the values of all flags before starting the JVM. This can be used to print the defaults for the heap and stack size settings as follows:</p> <li>
For Linux, Unix, Solaris and Mac OSX
$ java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
</li> <li>
For Windows:
<blockquote>
java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
</blockquote></li> <p>The output of the above commands will resemble the following:</p> <div class="language-java extra-class"><pre class="language-java"><code>uintx <span class="token class-name">InitialHeapSize</span> <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">20655360</span> <span class="token punctuation">{</span>product<span class="token punctuation">}</span>
uintx <span class="token class-name">MaxHeapSize</span> <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">331350016</span> <span class="token punctuation">{</span>product<span class="token punctuation">}</span>
uintx <span class="token class-name">PermSize</span> <span class="token operator">=</span> <span class="token number">21757952</span> <span class="token punctuation">{</span>pd product<span class="token punctuation">}</span>
uintx <span class="token class-name">MaxPermSize</span> <span class="token operator">=</span> <span class="token number">85983232</span> <span class="token punctuation">{</span>pd product<span class="token punctuation">}</span>
intx <span class="token class-name">ThreadStackSize</span> <span class="token operator">=</span> <span class="token number">1024</span> <span class="token punctuation">{</span>pd product<span class="token punctuation">}</span>
</code></pre></div><p>The sizes are given in bytes.</p> <h2 id="garbage-collection"><a href="#garbage-collection" class="header-anchor">#</a> Garbage collection</h2> <h3 id="the-c-approach-new-and-delete"><a href="#the-c-approach-new-and-delete" class="header-anchor">#</a> The C++ approach - new and delete</h3> <p>In a language like C++, the application program is responsible for managing the memory used by dynamically allocated memory. When an object is created in the C++ heap using the <code>new</code> operator, there needs to be a corresponding use of the <code>delete</code> operator to dispose of the object:</p> <li>
If program forgets to `delete` an object and just "forgets" about it, the associated memory is lost to the application. The term for this situation is a **memory leak**, and it too much memory leaks an application is liable to use more and more memory, and eventually crash.
</li> <li>
On the other hand, if an application attempts to `delete` the same object twice, or use an object after it has been deleted, then the application is liable to crash due to problems with memory corruption
</li> <p>In a complicated C++ program, implementing memory management using <code>new</code> and <code>delete</code> can be time consuming. Indeed, memory management is a common source of bugs.</p> <h3 id="the-java-approach-garbage-collection"><a href="#the-java-approach-garbage-collection" class="header-anchor">#</a> The Java approach - garbage collection</h3> <p>Java takes a different approach. Instead of an explicit <code>delete</code> operator, Java provides an automatic mechanism known as garbage collection to reclaim the memory used by objects that are no longer needed. The Java runtime system takes responsibility for finding the objects to be disposed of. This task is performed by a component called a <strong>garbage collector</strong>, or GC for short.</p> <p>At any time during the execution of a Java program, we can divide the set of all existing objects into two distinct subsets<sup>1</sup>:</p> <li>
Reachable objects are defined by the JLS as follows:
<blockquote>
A reachable object is any object that can be accessed in any potential continuing computation from any live thread.
</blockquote>
In practice, this means that there is a chain of references starting from an in-scope local variable or a `static` variable by which some code might be able to reach the object.
</li> <li>
Unreachable objects are objects that **cannot possibly** be reached as above.
</li> <p>Any objects that are unreachable are <strong>eligible</strong> for garbage collection. This does not mean that they <strong>will</strong> be garbage collected. In fact:</p> <ul><li>An unreachable object <strong>does not</strong> get collected immediately on becoming unreachable<sup>1</sup>.</li> <li>An unreachable object <strong>may not ever</strong> be garbage collected.</li></ul> <p>The Java language Specification gives a lot of latitude to a JVM implementation to decide when to collect unreachable objects. It also (in practice) gives permission for a JVM implementation to be conservative in how it detects unreachable objects.</p> <p>The one thing that the JLS guarantees is that no <strong>reachable</strong> objects will ever be garbage collected.</p> <h3 id="what-happens-when-an-object-becomes-unreachable"><a href="#what-happens-when-an-object-becomes-unreachable" class="header-anchor">#</a> What happens when an object becomes unreachable</h3> <p>First of all, nothing specifically happens when an object <strong>becomes</strong> unreachable. Things only happen when the garbage collector runs <strong>and</strong> it detects that the object is unreachable. Furthermore, it is common for a GC run to not detect all unreachable objects.</p> <p>When the GC detects an unreachable object, the following events can occur.</p> <li>
If there are any `Reference` objects that refer to the object, those references will be cleared before the object is deleted.
</li> <li>
If the object is **finalizable**, then it will be finalized. This happens before the object is deleted.
</li> <li>
The object can be deleted, and the memory it occupies can be reclaimed.
</li> <p>Note that there is a clear sequence in which the above events <strong>can</strong> occur, but nothing requires the garbage collector to perform the final deletion of any specific object in any specific time-frame.</p> <h3 id="examples-of-reachable-and-unreachable-objects"><a href="#examples-of-reachable-and-unreachable-objects" class="header-anchor">#</a> Examples of reachable and unreachable objects</h3> <p>Consider the following example classes:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token comment">// A node in simple "open" linked-list.</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Node</span> <span class="token punctuation">{</span>
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">int</span> counter <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">int</span> nodeNumber <span class="token operator">=</span> <span class="token operator">++</span>counter<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token class-name">Node</span> next<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ListTest</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// M1</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">prinln</span><span class="token punctuation">(</span><span class="token string">"Done"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// M2</span>
<span class="token punctuation">}</span>
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">Node</span> n1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// T1</span>
<span class="token class-name">Node</span> n2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// T2</span>
<span class="token class-name">Node</span> n3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Node</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// T3</span>
n1<span class="token punctuation">.</span>next <span class="token operator">=</span> n2<span class="token punctuation">;</span> <span class="token comment">// T4</span>
n2 <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// T5</span>
n3 <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// T6</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Let us examine what happens when <code>test()</code> is called. Statements T1, T2 and T3 create <code>Node</code> objects, and the objects are all reachable via the <code>n1</code>, <code>n2</code> and <code>n3</code> variables respectively. Statement T4 assigns the reference to the 2nd <code>Node</code> object to the <code>next</code> field of the first one. When that is done, the 2nd <code>Node</code> is reachable via two paths:</p> <div class="language- extra-class"><pre class="language-text"><code>
n2 -> Node2
n1 -> Node1, Node1.next -> Node2
</code></pre></div><p>In statement T5, we assign <code>null</code> to <code>n2</code>. This breaks the first of the reachability chains for <code>Node2</code>, but the second one remains unbroken, so <code>Node2</code> is still reachable.</p> <p>In statement T6, we assign <code>null</code> to <code>n3</code>. This breaks the only reachability chain for <code>Node3</code>, which makes <code>Node3</code> unreachable. However, <code>Node1</code> and <code>Node2</code> are both still reachable via the <code>n1</code> variable.</p> <p>Finally, when the <code>test()</code> method returns, its local variables <code>n1</code>, <code>n2</code> and <code>n3</code> go out of scope, and therefore cannot be accessed by anything. This breaks the remaining reachability chains for <code>Node1</code> and <code>Node2</code>, and all of the <code>Node</code> objects are nor unreachable and <strong>eligible</strong> for garbage collection.</p> <p><sup>1 - This is a simplification that ignores finalization, and <code>Reference</code> classes.</sup> <sup>2 - Hypothetically, a Java implementation could do this, but the performance cost of doing this makes it impractical.</sup></p> <h2 id="memory-leaks-in-java"><a href="#memory-leaks-in-java" class="header-anchor">#</a> Memory leaks in Java</h2> <p>In the <a href="http://stackoverflow.com/documentation/java/2804/java-memory-management/9473/garbage-collection#t=201610151259592490802" target="_blank" rel="noopener noreferrer">Garbage collection<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> example, we implied that Java solves the problem of memory leaks. This is not actually true. A Java program can leak memory, though the causes of the leaks are rather different.</p> <h3 id="reachable-objects-can-leak"><a href="#reachable-objects-can-leak" class="header-anchor">#</a> Reachable objects can leak</h3> <p>Consider the following naive stack implementation.</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NaiveStack</span> <span class="token punctuation">{</span>
<span class="token keyword">private</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> stack <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">private</span> <span class="token keyword">int</span> top <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">push</span><span class="token punctuation">(</span><span class="token class-name">Object</span> obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>top <span class="token operator">>=</span> stack<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">StackException</span><span class="token punctuation">(</span><span class="token string">"stack overflow"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
stack<span class="token punctuation">[</span>top<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> obj<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>top <span class="token operator"><=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">StackException</span><span class="token punctuation">(</span><span class="token string">"stack underflow"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> stack<span class="token punctuation">[</span><span class="token operator">--</span>top<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> top <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>When you <code>push</code> an object and then immediately <code>pop</code> it, there will still be a reference to the object in the <code>stack</code> array.</p> <p>The logic of the stack implementation means that that reference cannot be returned to a client of the API. If an object has been popped then we can prove that it cannot <strong>"be accessed in any potential continuing computation from any live thread"</strong>. The problem is that a current generation JVM cannot prove this. Current generation JVMs do not consider the logic of the program in determining whether references are reachable. (For a start, it is not practical.)</p> <p>But setting aside the issue of what <strong>reachability</strong> really means, we clearly have a situation here where the <code>NaiveStack</code> implementation is "hanging onto" objects that ought to be reclaimed. That is a memory leak.</p> <p>In this case, the solution is straightforward:</p> <div class="language- extra-class"><pre class="language-text"><code>
public Object pop() {
if (top <= 0) {
throw new StackException("stack underflow");
}
Object popped = stack[--top];
stack[top] = null; // Overwrite popped reference with null.
return popped;
}
</code></pre></div><h3 id="caches-can-be-memory-leaks"><a href="#caches-can-be-memory-leaks" class="header-anchor">#</a> Caches can be memory leaks</h3> <p>A common strategy for improving service performance is to cache results. The idea is that you keep a record of common requests and their results in an in-memory data structure known as a cache. Then, each time a request is made, you lookup the request in the cache. If the lookup succeeds, you return the corresponding saved results.</p> <p>This strategy can be very effective if implemented properly. However, if implemented incorrectly, a cache can be a memory leak. Consider the following example:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RequestHandler</span> <span class="token punctuation">{</span>
<span class="token keyword">private</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">,</span> <span class="token class-name">Result</span><span class="token punctuation">></span></span> cache <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token class-name">Result</span> <span class="token function">doRequest</span><span class="token punctuation">(</span><span class="token class-name">Task</span> task<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">Result</span> result <span class="token operator">=</span> cache<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>task<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>result <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
result <span class="token operator">==</span> <span class="token function">doRequestProcessing</span><span class="token punctuation">(</span>task<span class="token punctuation">)</span><span class="token punctuation">;</span>
cache<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>task<span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> result<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>The problem with this code is that while any call to <code>doRequest</code> could add a new entry to the cache, there is nothing to remove them. If the service is continually getting different tasks, then the cache will eventually consume all available memory. This is a form of memory leak.</p> <p>One approach to solving this is to use a cache with a maximum size, and throw out old entries when the cache exceeds the maximum. (Throwing out the least recently used entry is a good strategy.) Another approach is to build the cache using <code>WeakHashMap</code> so that the JVM can evict cache entries if the heap starts getting too full.</p> <h2 id="finalization"><a href="#finalization" class="header-anchor">#</a> Finalization</h2> <p>A Java object may declare a <code>finalize</code> method. This method is called just before Java releases the memory for the object. It will typically look like this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyClass</span> <span class="token punctuation">{</span>
<span class="token comment">//Methods for the class</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">finalize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Throwable</span> <span class="token punctuation">{</span>
<span class="token comment">// Cleanup code</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>However, there some important caveats on the behavior of Java finalization.</p> <ul><li>Java makes no guarantees about when a <code>finalize()</code> method will called.</li> <li>Java does not even guarantee that a <code>finalize()</code> method will be called some time during the running application's lifetime.</li> <li>The only thing that is guaranteed is that the method will be called before the object is deleted ... if the object is deleted.</li></ul> <p>The caveats above mean that it is a bad idea to rely on the <code>finalize</code> method to perform cleanup (or other) actions that must be performed in a timely fashion. Over reliance on finalization can lead to storage leaks, memory leaks and other problems.</p> <p>In short, there are very few situation where finalization is actually a good solution.</p> <h3 id="finalizers-only-run-once"><a href="#finalizers-only-run-once" class="header-anchor">#</a> Finalizers only run once</h3> <p>Normally, an object is deleted after it has been finalized. However, this doesn't happen all of the time. Consider the following example<sup>1</sup>:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CaptainJack</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">CaptainJack</span> notDeadYet <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">finalize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Resurrection!</span>
notDeadYet <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>When an instance of <code>CaptainJack</code> becomes unreachable and the garbage collector attempts to reclaim it, the <code>finalize()</code> method will assign a reference to the instance to the <code>notDeadYet</code> variable. That will make the instance reachable once more, and the garbage collector won't delete it.</p> <p>Question: Is Captain Jack immortal?</p> <p>Answer: No.</p> <p>The catch is the JVM will only run a finalizer on an object once in its lifetime. If you assign <code>null</code> to <code>notDeadYet</code> causing a resurected instance to be unreachable once more, the garbage collector won't call <code>finalize()</code> on the object.</p> <p><sup>1 - See <a href="https://en.wikipedia.org/wiki/Jack_Harkness" target="_blank" rel="noopener noreferrer">https://en.wikipedia.org/wiki/Jack_Harkness<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>.</sup></p> <h2 id="manually-triggering-gc"><a href="#manually-triggering-gc" class="header-anchor">#</a> Manually triggering GC</h2> <p>You can manually trigger the Garbage Collector by calling</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">gc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>However, Java does not guarantee that the Garbage Collector has run when the call returns. This method simply "suggests" to the JVM (Java Virtual Machine) that you want it to run the garbage collector, but does not force it to do so.</p> <p>It is generally considered a bad practice to attempt to manually trigger garbage collection. The JVM can be run with the <code>-XX:+DisableExplicitGC</code> option to disable calls to <code>System.gc()</code>. Triggering garbage collection by calling <code>System.gc()</code> can disrupt normal garbage management / object promotion activities of the specific garbage collector implementation in use by the JVM.</p> <h4 id="remarks"><a href="#remarks" class="header-anchor">#</a> Remarks</h4> <p>In Java, objects are allocated in the heap, and heap memory is reclaimed by automatic garbage collection. An application program cannot explicitly delete a Java object.</p> <p>The basic principles of Java garbage collection are described in the <a href="http://stackoverflow.com/documentation/java/2804/java-memory-management/9473/garbage-collection#t=201610151259592490802" target="_blank" rel="noopener noreferrer">Garbage collection<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> example. Other examples describe finalization, how to trigger the garbage collector by hand, and the problem of storage leaks.</p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/java/java-memory-management.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/java/jvm-tool-interface.html" class="prev">
JVM Tool Interface
</a></span> <span class="next"><a href="/java/java-performance-tuning.html">
Java Performance Tuning
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/1591.975d1834.js" defer></script>
</body>
</html>