Using the Limits
Trail: Java API for XML Processing (JAXP)
Lesson: Processing Limits

Using the Limits

Environment Evaluation

Evaluation includes, at the system level, the amount of memory available for applications, whether XML, XSD, or XSL sources from untrusted sources are accepted and processed, and at the application level, whether certain constructs such as DTDs are used.

Memory Setting and Limits

XML processing can be very memory intensive. The amount of memory allowed depends on the specific requirements of applications. Maximum memory allowed for an environment depends on the requirements of applications, and the need to prevent excessive memory usage by processing malformed XML data.

The default limits are generally set to allow legitimate XML inputs for most of applications with memory usage allowed for a small hardware system (such as a PC). It is recommended that the limits are set to the smallest possible values, so that any malformed input can be caught earlier without consuming large amount of memory.

Estimating the Limits Using the getEntityCountInfo Property

To help users analyze what are the proper limits, a special property called is available. The following code snippet shows an example of using the property:

parser.setProperty("", "yes");

See Samples for more information on downloading the example code.

When the program is run with the DTD in W3C MathML 3.0, it prints out the following table:

Property Limit Total Size Size Entity Name
ENTITY_EXPANSION_LIMIT 64000 1417 0 null
MAX_OCCUR_NODE_LIMIT 5000 0 0 null
TOTAL_ENTITY_SIZE_LIMIT 50000000 55425 0 null
PARAMETER_ENTITY_SIZE_LIMIT 1000000 0 7303 %MultiScriptExpression

In this example, the total number of entity references, or the entity expansion, is 1417; the default limit is 64000. The total size of all entities is 55425; the default limit is 50000000. The biggest parameter entity is %MultiScriptExpression with a length of 7303 after all references are resolved; the default limit is 1000000.

If this is the largest file that the application is expected to process, it is recommended that the limits be set to smaller numbers. For example, 2000 for ENTITY_EXPANSION_LIMIT, 100000 for TOTAL_ENTITY_SIZE_LIMIT, and 10000 for PARAMETER_ENTITY_SIZE_LIMIT.

Setting Limits

Limits can be set in the same way as other JAXP properties. They can be set through factory methods, or through the parser:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setAttribute(name, value);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
parser.setProperty(name, value);
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(name, value);
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
schemaFactory.setProperty(name, value);
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(name, value);

The following example shows how to set limits using DocumentBuilderFactory:

dbf.setAttribute(JDK_ENTITY_EXPANSION_LIMIT, "2000");
dbf.setAttribute(TOTAL_ENTITY_SIZE_LIMIT, "100000");
dbf.setAttribute(PARAMETER_ENTITY_SIZE_LIMIT, "10000"); 
dbf.setAttribute(JDK_MAX_ELEMENT_DEPTH, "100"); 

Using System Properties

System properties may be useful if changing code is not feasible.

To set limits for an entire invocation of the JDK or JRE, set the system properties on the command line. To set the limits for only a portion of the application, the system properties may be set before the section and cleared afterwards. The following code shows how system properties may be used:

public static final String SP_GENEAL_ENTITY_SIZE_LIMIT = "jdk.xml.maxGeneralEntitySizeLimit";

//set limits using system property
System.setProperty(SP_GENEAL_ENTITY_SIZE_LIMIT, "2000");

//this setting will affect all processing after it's set

//after it is done, clear the property

See Samples for more information on downloading the example code.

Using the File

The file is a plain configuration file. It is usually located at ${java.home}/lib/ where java.home is the JRE install directory, e.g., [path to installation directory]/jdk7/jre.

A limit can be set by adding the following line to the file:


Note that the property name is the same as that of the system property and has the prefix jdk.xml.

When the property is set in the file, all invocations of the JDK and JRE will observe the limit.

Previous page: Scope and Order
Next page: Error Handling