aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes OutOfMemory error with long JList using a CellRenderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "OutOfMemory error with long JList using a CellRenderer" Watch "OutOfMemory error with long JList using a CellRenderer" New topic
Author

OutOfMemory error with long JList using a CellRenderer

Peter Seidel
Greenhorn

Joined: Jan 21, 2011
Posts: 9
Hello, I am new here!

I am trying to get some help. I hope I can find it here.

I have a JList with a CellRenderer. I do add a lot of list entries to it.
If I do this, I am often getting an OutOfMemory error!

By debugging the code, I found out, that adding the entries to the list itself works well - but if the list is rendered, the problem occurs.
So there is nothing I can do about it. I can try to make the CellRenderer use less memory or tike it out completely (but the CellRenderer is needed, for comforable reading!). - But this would just change the list entry limit: if I add even more entries, I will get the same problem again.

I heard, that in SWT it is possible to use a VIRTUAL flag: if this style is used, the list is not rendered completely, but only the area which is visible at moment. I guess this list is not so responsive like with another style, but it should not need so much memory. Is this correct and can I do something similar with Swing? If yes, how can I do it?

I really appreciate your help. Thank you!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

1) Define "a lot of entries". Are we talking thousands, millions, ...?

2) Please post the renderer code. I've never seen a renderer causing memory problems before, so perhaps you have a memory leak in there somewhere.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Seidel
Greenhorn

Joined: Jan 21, 2011
Posts: 9
Rob Spoor wrote:1) Define "a lot of entries". Are we talking thousands, millions, ...?

Hello Rob, we talking about 100'000+.

Rob Spoor wrote:2) Please post the renderer code. I've never seen a renderer causing memory problems before, so perhaps you have a memory leak in there somewhere.

Sure, I will do that - here is the CellRenderer:



I need the CellRenderer for tabulator support. I also have to be able to set the tab size.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
While I don't see where your implementation can cause the OOME there is one thing that could be improved. The DefaultListCellRenderer IS-A JLabel, but you use another JTextArea to delegate the calls to. Maybe it would be easier to implement the interface ListCellRenderer directly? Even if you had to copy the logic for retrieving colors.

But this should not be the problem here, 100.000+ entries should be no problem as the renderer is instantiated only once and called only for the items visible. I guess the leak is within the model or the evaluation of "value" (e.g. a faulty toString?).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Have you only implemented that custom renderer so you can use tab and shift-tab to go the the next / previous cell? If so, I think you may wish to change the default tabbing behaviour of the JList. Using the InputMap and ActionMap you can define your own actions for any keystroke, including tab and shift-tab. You can call the default behaviour (by calling the original Action's actionPerformed method) if you are pressing tab when on the last cell or pressing shift-tab when on the first cell, or if no cell is selected at all.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
I think the OP was talking about tabs rendering within the text displayed with the renderer itself.
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
There might be a leak in PlainDocument class (JTextArea uses). Try to replace this

with
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

j fck wrote:
I heard, that in SWT it is possible to use a VIRTUAL flag: if this style is used, the list is not rendered completely, but only the area which is visible at moment. I guess this list is not so responsive like with another style, but it should not need so much memory. Is this correct and can I do something similar with Swing? If yes, how can I do it?


That's how Swing lists behave already; only the visible rows are painted.


[Jess in Action][AskingGoodQuestions]
Peter Seidel
Greenhorn

Joined: Jan 21, 2011
Posts: 9
Hello, thank you to all!

I changed my code like Stanislav suggested and loaded a big file: it took a while, but it work well.
So I think, my problem is solved.

I will test it for a while now - if I am facing more problems, I will come back, since here are the pros!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

I suggest you first follow the instructions in the private message Campbell Ritchie has sent you about three hours ago.
Peter Seidel
Greenhorn

Joined: Jan 21, 2011
Posts: 9
Hello, I was too optimistic: the problem is not gone...
Does someone of you have another idea, which could solve my problem?
Thank you in advance!


EDIT:
Hello Hauke Ingmar Schmidt, can you please tell me, how I can copy the colors and font style?
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
Try to use a profiler (e.g. JProfiler) to understand what instances of what classes aren't removed. Then see how they appear in you app. Where they created and why. Is there any way to reuse or free them.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Peter Seidel wrote:can you please tell me, how I can copy the colors and font style?


Please have a look at the javax.swing.DefaultListcellRenderer source code to see how it is done, especially the getListCellRendererComponent method.
Peter Seidel
Greenhorn

Joined: Jan 21, 2011
Posts: 9
Hauke Ingmar Schmidt wrote:Please have a look at the javax.swing.DefaultListcellRenderer source code to see how it is done, especially the getListCellRendererComponent method.

Hello, thank you for your answer. Unfortunately the link is not working... and the Java doc site has no example.

I stuck here:


The font is good - but I cannot select list entries and there is no space to other list entries.
Would be good, if you can help me here.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
My fault, the C in cell should be capitalized. Just open the type in your IDE to look at the source.

You can select cells, but you will not see the selection with your code. You have to take care of that for yourself when writing an own renderer. It is all quite readable in the source of the DefaultListCellRenderer.
Peter Seidel
Greenhorn

Joined: Jan 21, 2011
Posts: 9
Hauke Ingmar Schmidt wrote:Just open the type in your IDE to look at the source.

Do you mean clicking the type while pressing the Ctrl key?
- This does bring me an error page :( "Source not found" - I am using eclispe).

Hauke Ingmar Schmidt wrote:You can select cells, but you will not see the selection with your code. You have to take care of that for yourself when writing an own renderer. It is all quite readable in the source of the DefaultListCellRenderer.

Sorry, I can't see it from the Java doc page though.
I have no experience in this - I did never draw something myself...
Could you please give me an example for drawing the selection?
How do I get the selection color of the system (native look and feel)?
PS: to make it a little easier, I don't need the focus frame! :)
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
That is something you need to fix. You need the source, you need the Javadocs.

By default Eclipse uses an installed JRE for building, not a JDK. You should change this. Download the JDK, install it, set it as the default Java Runtime in the Eclipse settings, then you can look into the source. It is pretty straightforward, getting the default values for colours and borders from the Swing "registry" and setting them dependent on the state.

After that, the easiest, albeit not very nice way is to just copy the DefaultListCellRenderer source completely and just change the class it derives from to the text component that fulfills your need of tab rendering.
Peter Seidel
Greenhorn

Joined: Jan 21, 2011
Posts: 9
Ok, I will do that. - But can you tell me please, where I have to change this setting?
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Window -> Preferences, Java -> Installed JREs. Add the JDK, make it default (check it), delete the old entry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OutOfMemory error with long JList using a CellRenderer