Search code examples
memory-leaksjava-8swtrcp

SWT uses operating system resources, but what is the limit and how to profile that?


SWT uses operating system resources so the SWT memory consumption doesn't depend on the heap (xms xms) and on the non-heap (metaspace). correct me if I'm wrong on that point.

  • is there any limit to the resources used by SWT components (colors, fonts, images...)?

  • how to know if the limit is reached and how to profile that?

  • if this limit is reached, a Java RCP application can crash without a java OutOfMemoryError (just a pid file)?

P.S.: I use Sleak to tack the amount of graphic objects currently used by the application


Solution

  • As Baz mentioned, the maximum number of GDI handles, which is specific to Windows, influences how many resources a (SWT) process can allocate. See the this question for more details. On other platforms, the limit may be different.

    As you already found out, SWT Sleak is the right tool to monitor graphics resource usage of SWT applications.

    If an application runs out of handles, it does not just 'crash'. An SWTError is raised when the limit is reached and an attempt is made to create a new resource.

    In general, the exception can be handled like any other exception and after releasing unused resources, new ones can be created.

    IIRC, the default exception handler of RCP applications will open a dialog that asks the user to exit the application gracefully in this case.

    However, running out of handles in an SWT application is usually a sign that your strategy of how widgets are created/used is wrong.

    A common strategy to reduce the number of resources is to use lazy/virtual Trees and Tables and to create widgets only when they become visible and dispose of them when no longer needed. In a TabFolder, for example, you would defer populating the TabItem unless it gets selected.