| Start |
|---|
| Setting the Heap, PermGen and Stack sizes 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 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). 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. Setting the Heap, PermGen and default Stack sizes: The following JVM options set the heap size:
The Examples: Finding the default sizes: The java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize" The output of the above commands will resemble the following: The sizes are given in bytes. |
Garbage collection The C++ approach - new and deleteIn 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 In a complicated C++ program, implementing memory management using The Java approach - garbage collectionJava takes a different approach. Instead of an explicit At any time during the execution of a Java program, we can divide the set of all existing objects into two distinct subsets1: A reachable object is any object that can be accessed in any potential continuing computation from any live thread.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. Any objects that are unreachable are eligible for garbage collection. This does not mean that they will be garbage collected. In fact:
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. The one thing that the JLS guarantees is that no reachable objects will ever be garbage collected. What happens when an object becomes unreachableFirst of all, nothing specifically happens when an object becomes unreachable. Things only happen when the garbage collector runs and it detects that the object is unreachable. Furthermore, it is common for a GC run to not detect all unreachable objects. When the GC detects an unreachable object, the following events can occur. Note that there is a clear sequence in which the above events can occur, but nothing requires the garbage collector to perform the final deletion of any specific object in any specific time-frame. Examples of reachable and unreachable objectsConsider the following example classes: Let us examine what happens when In statement T5, we assign In statement T6, we assign Finally, when the 1 - This is a simplification that ignores finalization, and |
| Memory leaks in Java In the Garbage collection (opens new window) 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. Reachable objects can leakConsider the following naive stack implementation. When you 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 "be accessed in any potential continuing computation from any live thread". 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.) But setting aside the issue of what reachability really means, we clearly have a situation here where the In this case, the solution is straightforward: Caches can be memory leaksA 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. This strategy can be very effective if implemented properly. However, if implemented incorrectly, a cache can be a memory leak. Consider the following example: The problem with this code is that while any call to 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 |
| Finalization A Java object may declare a However, there some important caveats on the behavior of Java finalization.
The caveats above mean that it is a bad idea to rely on the In short, there are very few situation where finalization is actually a good solution. Finalizers only run onceNormally, an object is deleted after it has been finalized. However, this doesn't happen all of the time. Consider the following example1: When an instance of Question: Is Captain Jack immortal? Answer: No. The catch is the JVM will only run a finalizer on an object once in its lifetime. If you assign 1 - See https://en.wikipedia.org/wiki/Jack_Harkness (opens new window). |
| Manually triggering GC You can manually trigger the Garbage Collector by calling 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. It is generally considered a bad practice to attempt to manually trigger garbage collection. The JVM can be run with the RemarksIn 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. The basic principles of Java garbage collection are described in the Garbage collection (opens new window) example. Other examples describe finalization, how to trigger the garbage collector by hand, and the problem of storage leaks. |
Read Article
https://develop.horje.com/learn/1434/reference