Thursday, 3 November 2011

About Garbage Collection

 

Frequently Asked Questions
about Garbage Collection
in the HotspotTM JavaTM Virtual Machine

This document describes the behavior of the Java(tm) HotSpot(tm) virtual machine. This behavior is not part of the VM specification, however, and is subject to change in future releases. Moreover the behavior described here is generic behavior and will not apply to the execution of all Java applications.

1.      How is the generational collector implemented in HotSpot(tm)?

The default collector in HotSpot has two generations: the young generation and the tenured generation. Most allocations are done in the young generation. The young generation is optimized for objects that have a short lifetime relative to the interval between collections. Objects that survive several collections in the young generation are moved to the tenured generation. The young generation is typically smaller and is collected more often. The tenured generation is typically larger and collected less often.

The young generation collector is a copying collector. The young generation is divided into 3 spaces: eden-space, to-space, and from-space. Allocations are done from eden-space and from-space. When those are full a young generation is collection is done. The expectation is that most of the objects are garbage and any surviving objects can be copied to to-space. If there are more surviving objects than can fit into to-space, the remaining objects are copied into the tenured generation. There is an option to collect the young generation in parallel.

The tenured generation is collected with a mark-sweep-compact collection. There is an option to collect the tenured generation concurrently.

2.      What is the relevance of -XX:MaxNewSize? Where will the differences between -XX:NewSize and -XX:MaxNewSize grow, Eden or Survivor Spaces?

The young generation is set by a policy that bounds the size from below by NewSize and bounds it from above by MaxNewSize. As the young generation grows from NewSize to MaxNewSize, both eden and the survivor spaces grow.

3.      Are all eden-space objects moved into the survivor space so that after a minor gc, eden-space is empty?

Yes. If all the live objects in eden do no fit into a survivor space, the remaining live objects are promoted into the old generation.

4.      When using -XX:TargetSurvivorRatio=90 will this leave ten percent of to-space for objects to be moved from eden?

No. It means that a tenuring threshold is chosen so that, based on the ages of what was scavenged in the last minor collection, there should be nearly 90% of the survivor size used. The actual amount scavenged from either the survivor space or eden may be considerably more or less.

TargetSurvivorRatio does not usually make a big difference.
5.      If objects in eden-space require more space than is available in the to-survivor space, will eden-space objects have precedence over from-survivor space objects? How does the age of from-survivor space objects affect promotion?

There is no distinction here between what comes from eden and what comes from the from-survivor space. After a minor collection completes, both eden and the from-survivor space are empty. If the to-survivor space fills up, any remaining objects are promoted directly into the old generation regardless of their age or origin.


6.      Between NewSize and NewRatio which option takes precedence?
In jdk 1.4.1 and later, neither has strict precedence. The maximum of NewSize and the size calculated using NewRatio is used. The formula is

min(MaxNewSize, max(NewSize, heap/(NewRatio+1)))

7.      How should the permanent generation be sized?
The permanent generation is used to hold reflective of the VM itself such as class objects and method objects. These reflective objects are allocated directly into the permanent generation, and it is sized independently from the other generations. Generally, sizing of this generation can be ignored because the default size is adequate. However, programs that load many classes may need a larger permanent generation.

8.      How can I tell if the permanent generation is filling up?

Starting in 1.4.2 -XX:+PrintGCDetails will print information about all parts of the heap collected at each garbage collection. For a full collection

[Full GC [Tenured: 30437K->33739K(280576K), 0.7050569 secs] 106231K->33739K(362112K), [Perm : 2919K->2919K(16384K)], 0.7052334 secs]

this example shows that little was collected in the permanent generation (it went from 2919K used before the collection to 2919K used after the collection) and the current size of the permanent generation is 16384K.

9.      How can I increase the permanent generation size?

Use the command line option -XX:MaxPermSize=<desired size>

10.  How do I know what classes are being loaded or unloaded?

Use the command line options -XX:+TraceClassloading and -XX:+TraceClassUnloading

11.  What is the best size for the young generation?

The young generation should be sized large enough so that short-lived objects have a chance to die before the next young generation collection. This is a tradeoff since a larger young generation will allow more time for objects to die but may also take longer to collect. Experiment with the size of the young generation to optimize the young generation collection time or the application throughput.


19.  Can I see how much of a thread allocation buffer is being left unused?

There's a flag, -XX:+PrintTLAB,that will trace all the operations on TLAB's. In particular, it prints lines like

reset TLAB: thread: 0x0002d7d0 size: 8KB unused: 76B Total fragmentation 0.004499

each time a TLAB is filled with a int[]. In this case, the unused trailing 76B will be unused.

This is an example of a TLAB that has filled up and a new one will be allocated. The amount of waste here is relatively small. More waste can occur in preparation for a garbage collection. For TLAB output that show up just before the a garbage collection like

reset TLAB: thread: 0x0002d840 size: 8KB unused: 7276B Total fragmentation 0.004580
[Full GC 10424K->591K(15688K), 0.1222677 secs]

where we are filling 7276 bytes of the 8192 byte TLAB. (The "Total fragmentation" is a cumulative accounting of the fragmentation caused by TLAB's.) TLAB's resize by default on SPARC -server, or if you use the -XX:+ResizeTLAB flag, so you may well get large TLAB's if you are running that JVM. Note that we aren't "wasting" the space for the fillers right before collections, as the collection will recover the space the filler objects occupy.

20.  Does the default of NewRatio change with the compiler?

On SPARC's, -XX:NewRatio defaults to 8 with -client and 2 with -server, so the ratio of the young generation to the old generation will be 1::8 and the young generation will be 1/9th of the heap in -client and 1::2 or 1/3rd of the heap with -server.


30.  Should I increase the size of the permanent generation in the client vm?

This will always be a judgment call. In general increasing the size of a generation (and this applies not just to the permanent generation) can reduce the incidence of a wide variety of problems However, this may cause other processes to excessively page and/or garbage collect or throw out-of-memory exceptions.

There are two failure modes to consider.

When raising MaxPermSize, it is possible that previously well behaved programs that used to garbage collect to recover the permanent generation space will die by endless paging. For the permanent generation this usually only happens with the heavy interning of temporary strings.

The other failure mode is that address space must be reserved for the permanent generation and this will reduce that available for the rest of the heap (the maximum -Xmx may then be too large). This will cause programs configured to use all available space to fail at initialization.

Permanent generation defaults in recent VM's.

Release

v1.3.1_06
v1.4.1_01
v1.4.2
Client
PermSize
1M
4M
4M
Server
PermSize
1M
4M
16M
Client
MaxPermSize
32M
64M
64M
Server
MaxPermSize
64M
64M
64M


31.  Should I pool objects to help GC? Should I call System.gc() periodically?

The answer to these is No!

Pooling objects will cause them to live longer than necessary. We strongly advise against object pools.

Don't call System.gc(). The system will make the determination of when it's appropriate to do garbage collection and generally has the information necessary to do a much better job of initiating a garbage collection. If you are having problems with the garbage collection (pause times or frequency), consider adjusting the size of the generations.

Deployment Plan



Deployment Plan

Overriding value using Deployment Plan

We can override the values of the deployement descriptors at the runtime using plan.xml. Following are the steps which can be followed to override context-root of the application specified in the weblogic.xml:

1) My weblogic.xml looks as follows:



I want to change the context-root from virtual to TestApp at runtime.

2) Create a deployment plan using weblogic.PlanGenerator as follows:

java weblogic.PlanGenerator -all -plan Plan.xml Virtual.war

This will generate Plan.xml outside the archived application.

3) Plan.xml contains the values which can be overridden in the deployment descriptor at runtime. We can override the value of context-root at runtime as follows:


4) Now deploy the application using weblogic.Deployer as follows:

java weblogic.Deployer -adminurl t3://localhost:7001 -user weblogic -password weblogic -redeploy -name Virtual -source Virtual.war -targets AdminServer -plan Plan.xml

5) Test the application using the following URL:

http://localhost:7001/TestApp

Default URL of application:
http://localhost:7001/Virtual

At runtime the value of context-root was overridden to TestApp

Cluster Unicast messaging mode


Cluster Unicast messaging mode
UNICAST messaging mode

WLS10.x onwards there are 2 messaging modes of cluster: Unicast and Multicast. According to the following document Unicast is the recommended messaging mode though there have been many issues reported.

http://download-llnw.oracle.com/docs/cd/E11035_01/wls100/cluster/features.html

a) In Unicast messaging mode, cluster members are split into groups and every group has a group leader. Cluster members communicate to the group leader when they need to send a broadcast message which is usually the heartbeat message. When the cluster members detect the failure of a group leader, the next oldest member becomes the group leader. All group leaders are connected to each other.

b) The frequency of communication in unicast mode is similar to the frequency of sending messages on multicast port.

Unicast messaging mode Model:

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDtmy4IYRH8hM-A_LbUgl_RNj_nW2vwtiBdzXyysRpnVETSSQVEhphRPjtXeYV5rLz2at1EkGAhAKDIC7zrs6j28RFtBbPbUkzgQ8s8YWJLP7SqVezCCOENWPiN71S_GLM75Z-YbIeA2Q/s320/Untitled.jpg










Unicast Configuration:

Default Network Channel:

While creating a cluster choose Unicast as the messaging mode. If we leave the ‘Unicast broadcast channel’ blank then default listening port of the server is used if no channel is configured.

Config.xml looks like follows when default network channel is in use:
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGjPgHvsFBfywXcm44nS3RpikbFp5zcauaULp9RyandDRjt65wFG_H1dHNsmixDvFrOz2xY2u__CwHbJt0Kfy_pG0rXkHmRXBVYo3GsmGkRmLX9iF8tYaZNdRAo6sMTq82eeMrZYz3q44/s320/Untitled.jpg






In the standard out of managed servers we will get the following message:
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiTBbVQC8PnC_E3FqX4th2krrS55w-M0BDgWPH0LFV_yaZNdotqhtyW8u_8U2fdaUGIimzEGAmSdDj8uD1y-Z2g8Ak2Pl5g_X5irfAsDDkIUJV8jLX828APrFlLO-XYb5BrHTuTTHJVvE/s320/Untitled.jpg



This means that the default channel will listen all the protocols.

Custom Network Channel:

For customer Network channels to be used with Unicast we need to create Network Channel by the same name under each server in cluster from Protocol > Channels
We can create custom network channels for different protocols. Protocol used by unicast is cluster-broadcast for communication. Use this custom channel name in the Unicast broadcast channel.

NOTE: For example, If you are configuring Network channel for different protocols say http, t3,iiop etc then cluster-broadcast will be one of the common protocol which will be default be configured along with other protocols. Hence, custom channel created for http, t3, iiop can be used as unicat broadcast channel.

Config.xml will have the following information where Channel-0 is the custom Network channel.

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhckzpI6deNyDZWCgnqV5GdJrB_x5xFdHkBF4OqUfIHsd0if2_iPAj1plX858UwP2sO-Pkj3KE7wd_YSkvoReQk-M50ZPjpxclP2VpWlqfbO5qGeKRzYrK4mApNGI2tGhBjzyUHmdv752E/s320/Untitled.jpg



Here I configured Channel-0 for http protocol which is used as Unicast broadcast channel and I get the following message in the standard out of managed server:

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRL79ya_uC54hsP5mWB2bhPLQvYhUw_n4VC69qxvR6SjcAsqRy5EWbC5qu2ViYVxs80fmKnoaXu4XVsN-vcqaeoIFdKrQ9a9Y9rawXRGwIsbRIFPMU95JaT4Ga2JynvYRrFF5AV-ulekU/s320/Untitled.jpg

While configuring channels other things that have that have to be taken care:

1) Listen Address, Listen port: If this is left blank then the server’s listen address will be taken as default address and port
2) External Address, External port: This is the address of firewall or loadbalancer if there is any.
3) HTTP enabled for this protocol: If you want that all http requests should be listen by the protocol for which network channel is configured then we need to keep this checked
4) Outbound Enabled: If this channel is going to listen to outbound traffic then this has to be checked

Refer: http://download.oracle.com/docs/cd/E15051_01/wls/docs103/config_wls/network.html#wp1058979

Unicast Debug Flag:
-Dweblogic.debug.DebugUnicastMessaging

Testing Unicast connectivity

In Unicast, we do not have any utility check network issues. We can suggest the customer to change the messaging mode to Multicast and check for network issues if there are any using the multicast utility

Multicast or IP multicast is a simple broadcast technology that enables multiple applications to subscribe to a given IP address and port number and listen for messages. The IP/port combination is setup when the cluster is defined and server instances uses multicast for JNDI updates andcluster heartbeats. So using java utils.MulticastTest -n server1 -a 224.x.x.x –p 9001 We just check whether all the Nodes
available in the Cluster is able to communicate or not. It is actually testing at the N/W level.
Acceleration towards basic WLS issues