Skip to main content
Skip table of contents

Memory Management

📌 Overview

This article explains how to reduce memory usage when using Mako.

(question) Issue Description

Often there is a requirement to iterate through the pages of a document where the content of the page needs to be read. Memory usage within Mako increases with each page read and for performance reasons, some of the memory is not automatically released when moving to the next page. As the number of pages in the document increases, the need to keeping memory usage low is beneficial or required. 

💡 Solution

There are two methods of freeing memory that may help.

Releasing the page

This method should be the first port of call to reduce memory usage.

  • Once a page's content has been read, some of the content will be cached. This can be released by calling IPage::release().

  • If a page is not edited, and the original source is available, the IPage's reference to the page content will be released. This is useful to release the memory associated with the page content if a page is not to be accessed again. The content may be safely requested again using getContent().

  • If the page content cannot be released, the call will be ignored.

  • If the page has been edited, it can be reverted first by calling IPage::revert() and then calling IPage::release().

Emptying Mako's cache

Mako's cache can be emptied by calling IJawsMako::freeMemory(). This can be used to inform Mako that system memory is being exhausted and will cause Mako to free up cached information and discard any unmodified pages.

⌨️ Sample Code

These code snippets in C++ and C# show how to release the page while getting text runs.

C++

C#

CPP
  IDocumentPtr document = assembly->getDocument();
  uint32 pagecount = document->getNumPages();
  
  // Iterate the pages to get text runs
  for (uint32 pageIndex = 0; pageIndex < pagecount; pageIndex++)
  {
      IPagePtr page = document->getPage(pagenumber);
      CTextRunVect textruns;
      page->getTextRuns(textruns);
      // Release memory
      page->revert();
      page->release();
  }
C#
IDocument document = assembly.getDocument();
uint pagecount = document.getNumPages();
 
// Iterate the pages to get text runs
for (uint pageIndex = 0; pageIndex < pagecount; pageIndex++)
{
    IPage page = document.getPage(pageIndex);
    CTextRunVect textruns = page.getTextRuns(textruns);

    // Release memory
    page.revert();
    page.release();
}

☑️ Conclusion

Effective memory management is crucial when working with large documents in Mako. By utilizing methods such as IPage::release() and IJawsMako::freeMemory(), users can significantly reduce memory usage and improve performance. The provided C++ and C# code snippets demonstrate practical applications of these methods, ensuring efficient memory handling during document processing.

📚 Additional Resources

If you need additional help, see our API documentation for detailed information on class/method usage, or raise a support ticket via our customer portal.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.