Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the size of a java class impact the performance of the application

I am doing a swing based application where I use JTable. I used DefaultCellEditor for one of the column which requires combo box selection. But DefaultCellEditor has many methods which I don't require. So I wrote a custom CellEditor by extending AbstractCellEditor where I have implemented only the required methods. My question is

(in general) if we have a class and if we don't require all the methods of that class is it fine to use it or is it good to write a custom class where we implement only those methods which we require? and

by using custom class will the performance (memory wise) of the application will be improved or it remains same as the class which has all the methods?

Any help will be appreciated.

like image 838
Amarnath Avatar asked Sep 18 '12 10:09

Amarnath


3 Answers

Unless you have seriously good grounds for believing that nothing else in the application including the JDK itself uses DefaultCellEditor, you are almost certainly wasting your time by making things actively worse.

You also need to consider that you might have saved maybe 100k of code in extreme cases, where typical JVMS use around a gigabyte to execute. So you may have saved 0.01% of code space. This is not a productive use of your time. The correct procedure is to test and measure where the time and space bottlenecks really are, after the fact. Programmers are notoriously poor at predicting these things.

like image 148
user207421 Avatar answered Sep 20 '22 16:09

user207421


The code (the actual bytecode) for this class is only loaded once, in the PermGen region of memory, regardless of how many objects of this type you instantiate.

I would not accept this code since you are duplicating functionality which is already available, and you're not adding functionality to AbstractCellEditor, you're reimplementing code in DefaultCellEditor which has already been (hopefully) tested by Oracle (or Sun).

As EJP said, it's not worth the time and potential to introduce bugs.

like image 42
Faelkle Avatar answered Sep 20 '22 16:09

Faelkle


If you create a custom class which contains fewer member objects, then the memory footprint will be lower. The number of methods wouldn't impact the size of the objects, just the size of the class.

In general, I would not prematurely optimise unless you determine that you actually have a problem (i.e. if you have thousands of instances of the object and heap/garbage collector log analysis reveals that you're thrashing the memory and/or have frequent of collections of the old space), because additional code means:

  • Additional maintenance (you'd need to ensure that your custom CellEditor is not buggy)
  • Additional effort in writing the custom code
  • Additional effort in testing the custom code

AFAIK, a CellEditor is instantiated as and when needed, so you wouldn't save much memory anyway.

like image 45
beny23 Avatar answered Sep 16 '22 16:09

beny23