> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/get-started/faq/pdfviewctrl-min-memory.md).

# Reduce memory consumption

World's #1 PDF SDK Library for Web, Mobile, Server, Desktop

## Change render cache size

In order to achieve smoother viewing experience, [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) keeps certain amount of invisible content. However, keeping invisible content increases memory usage. Android sets an upper bound on the VM heap size allowed for an app. By default, `PDFViewCtrl` is set to use a quarter of maximum VM heap size for keeping visible and invisible content. Based on the memory footprint of your app, you may want to customize it.

To customize the memory usage:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
long allowedMax = Runtime.getRuntime().maxMemory() / (1024 * 1024);
 //should NOT use close to allowedMax
pdfViewCtrl.setRenderedContentCacheSize((long) (allowedMax * 0.25));
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val allowedMax = Runtime.getRuntime().maxMemory() / (1024 * 1024)
//should NOT use close to allowed_max
pdfViewCtrl.setRenderedContentCacheSize((allowedMax * 0.25).toLong())
```

{% endcode %}
{% endtab %}
{% endtabs %}

where `pdfViewCtrl` is an instance of `PDFViewCtrl`.

{% hint style="info" %}
If you want to minimize the heap memory usage at the cost of viewing experience quality, you can set allowed\_max to 0. `PDFViewCtrl` will not keep any invisible content.
{% endhint %}

{% hint style="info" %}
`setRenderedContentCacheSize(long)` only controls the memory usage of VM heap size by `PDFViewCtrl`, but not the native memory that `PDFViewCtrl` might use.
{% endhint %}

## Prevent out of memory when rotating device

Question:

> We are experiencing a lot of out of memory errors when rotating the device multiple times. Since the Activity is destroyed while rotating, we get a new (quite heavy) PDFViewCtrl every time we rotate the device.

Answer:

`PDFViewCtrl` can handle rotation properly, it is recommended that you configure your activity to handle configuration changes. This way the view will not be recreated and it will be resized accordingly.

1. In your app `AndroidManifest.xml` file, add `android:configChanges` to your `activity` tag.

{% tabs %}
{% tab title="XML" %}
{% code lineNumbers="true" %}

```xml
<activity android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize" >
  </activity>
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. Override [`onConfigurationChanged()`](https://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged\(android.content.res.Configuration\)) in your activity:where `mPdfViewCtrl` is an instance of `PDFViewCtrl`.

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl.onConfigurationChanged(newConfig);
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
override fun onConfigurationChanged(newConfig: Configuration) {
  super.onConfigurationChanged(newConfig)
  mPdfViewCtrl?.onConfigurationChanged(newConfig)
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Increase Android heap size

On Android 3.0 and above, you can also increase the allowed heap size of an app.

In your app `AndroidManifest.xml` file, add the `android:largeHeap="true"` attribute to the `<application>` tag:

{% tabs %}
{% tab title="XML" %}
{% code lineNumbers="true" %}

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
  <application android:largeHeap="true" >
  </application>

</manifest>
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/get-started/faq/pdfviewctrl-min-memory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
