JTextpane consumes more memory

harishalwala
harishalwala used Ask the Experts™
on
Hi
I am using a swing application that use multiple JTextpanes that contains huge Text & background painting. When I open one after the other textpane from the tabs, it cosumes around 100M, but once I close the tab with textpane, the memory remains and doesnot release to GC.
Any solution please.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
After you no longer need JTextPane (after you close it), assign its variable to null - perhaps it will be garbage collected sooner.
You can even call garbage collecition after that your self with System.gc() - although it is usually not recommended

How do you know it does not realease memory?
It deoes not do it immediately - only after GC runs -  so you need to check after some time
Awarded 2011
Awarded 2011
Commented:

check this, for example about how to have garbage collection more efficient:
http://www.leepoint.net/notes-java/background/60garbage_collection.html

What you can do to improve garbage collection performance
Garbage consists of objects that can't be referenced by anyone -- there are no static, instance, or local variables that references them either directly or indirectly.

Assign null to variables that you are no longer using. This will, if there are no other references, allow this memory to be recycled (garbage collected). Because local variables are deallocated when a method returns, they are less of a problem. Variables with the longest lifetime are static variables, you should be careful to assign null to any that are no longer used, especially if they reference large data structures.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial