0

Information:

I have a database table with 200.000 entries.I am using an ArrayList which holds 100 Buttons each time(Each Button has the info from the table entry it matches).

When the right arrow key is pressed the ArrayList is being cleared and the next 100 entries of dataBase table are added(as Buttons).

What i want to say is that every time i search the dataBase or i press left or right keys the ArrayList is cleared and added with 100 new Button.

The Problem

Should i reuse the existing buttons passing different values or just clearing them and creating new ones?

If i create new Buttons it consumes more memory ?If i reuse the same Buttons it doesn't consume extra memory?

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • Do you mean that you have an `ArrayList` of buttons for the next and back buttons? Or are the entries shown as buttons? – Nic Jun 23 '16 at 03:56
  • @QPaysTaxes (The entries shown as buttons).I edited the question so it is clear. – GOXR3PLUS Jun 23 '16 at 05:00
  • Should be pretty simple to benchmark... I would assume re-using would be better, but I don't know by what margin. Also - I'm assuming the buttons (or most of them) are displayed? – Itai Jun 23 '16 at 05:08
  • @sillyfly yes are displayed.I had never done benchmark,so i should do that to check memory or speed? – GOXR3PLUS Jun 23 '16 at 05:15
  • 2
    Whichever matters to you most, although my assumption is that they will both be better with re-using. – Itai Jun 23 '16 at 05:20
  • 1
    I guess @sillyfly is right. With 100 simple `Button`s I don't think that there will be any significant difference at all, but still modifying the scene-graph is expensive so if it is easy to reuse, go with reuse. – DVarga Jun 23 '16 at 06:01
  • @DVarga but imagine doing this 100 times.I mean if i press 100 times the next button then it will be costly to create 100*100 = 10000 new Button objects totally – GOXR3PLUS Jun 23 '16 at 08:50
  • 1
    Reusing the buttons will almost certainly have *some* performance benefit; but as stated by other commentators whether it's enough to be worthwhile is only a question that you can answer after extensive profiling. Obviously, if you don't reuse buttons, more buttons will be created and the GC will run more frequently to reclaim memory from the ones that are no longer used. The trade-off is that the code to implement reusing them will be more complex and consequently harder to maintain. Only you can determine the balance of those pros and cons. – James_D Jun 23 '16 at 12:08

1 Answers1

1

It's hard to give a clearly response. You can test your own application for both cases.

First one view difference of memory, like this:

Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 

And test how fast your application will work, in header declare new variable:

Date start = new Date();

In end of app

new Date() - start;

And you will answer yourself.

Vladlen Gladis
  • 1,699
  • 5
  • 19
  • 41
  • 1
    `System.nanoTime` is better suited for benchmarking. See also http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime – Itai Jun 23 '16 at 08:49