Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OutOfMemory error with long JList using a CellRenderer

 
Peter Seidel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Seidel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the OP was talking about tabs rendering within the text displayed with the renderer itself.
 
Stanislav Lapitsky
Ranch Hand
Posts: 53
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be a leak in PlainDocument class (JTextArea uses). Try to replace this

with
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Seidel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you first follow the instructions in the private message Campbell Ritchie has sent you about three hours ago.
 
Peter Seidel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I will do that. - But can you tell me please, where I have to change this setting?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Window -> Preferences, Java -> Installed JREs. Add the JDK, make it default (check it), delete the old entry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic