tags:

views:

381

answers:

6

I want to ‪reduce‬ the CPU usage/ROM usage/RAM usage - generally‬, all system resources that my app uses - who doesn't? :)

For this reason I want to split the preferences window from the rest of the application, and let the preferences window to run as ‪independent‬ program.

The preferences program ‪should‬ write to a Property file(not a problem at all) and to send a "update signal" to the main program - which means it should call the update method (that i wrote) that found in the Main class.

How can I call the update method in the Main program from the preferences program?

To put it another way, is a way to build preferences window that take system resources just when the window appears?

Is this approach - of separating programs and let them talk to each other (somehow) - the right approach for speeding up my programs?

+1  A: 

You could create a ServerSocket in the main window and have the preferences app connect to that with a regular Socket the protocol to use may be extremely simple, but... I think you should really look for the second approach: to build preferences window that take system resources just when it's appear?

To do that, you have to build the window and all it resources until the user performs the Preferences action, save your file ( or pass the content to the main app ) and dispose all the resources of the preference window by making all of its reference non accessible. The garbage collector will handle the rest.

OscarRyz
+9  A: 

What you're describing sounds like Premature Optimisation. If you're writing something other than a toy application, it's important to be confident that your optimisations are actually addressing a real problem. Is your program running slowly? If so, have you run it through a profiler or otherwise identified where the poor performance is happening?

If you have identified that what you want to do will address your performance issue, I suggest you look at running the components concurrently in different threads, not different processes. Then your components can avoid blocking each other, you will be able to take advantage of multi-core processors and you do not take on the complexity and performance overhead of inter-process communication over network sockets and the like.

Brabster
You'll use less resources overall if you set the preferences in the same process over having a separate program. Just tear it down when you're done. The total file sizes will be smaller, the total memory footprint will be smaller, and it will require less CPU resources. Java will free the extra resources when you're done using them.
Marcus Adams
+1 for Premature Optimisation
Cam
I don't think separate threads makes sense here, since the UI code will ultimately all be run on the same thread. In fact if you invoke UI code from off of the UI thread, strange things can happen. See my answer.
Jay Askren
"you would launch a separate thread off of the EDT, and when it finishes run code on the EDT to do the UI update." - sounds like using threads to me, albeit with additional Swing-specific constraints over bog-standard multithreading. Good answer though, +1.
Brabster
+6  A: 

You can communicate back and forth using sockets. Here's a tutorial of how to do something similar..

Unfortunately, I don't think this is going to help you minimize CPU usage, RAM, etc... If anything it might increase the CPU usage, RAM usage etc, because you need to run two JVM's instead of one. Unless you have some incredibly complicated preferences window, it is not likely taking that many resources that you need to worry about it. By adding the network communication, you are just adding more complexity without adding any benefit.

Edit:

If you have read the book Filthy Rich Clients, one of the main points of the book is that Rich Effects do not need to be resource intensive. Most of the book is devoted to showing how to add cool effects to an app with out taking a lot of resources. Throughout the book they are very careful to time everything to show what takes a long time and what doesn't. This is crucial when making your app less resource hungry. Write your app, see what feels slow, add timing code to those particular items that are slow, and speed up those particular parts of the code. Check with your timing code to see if it is actually faster. Rinse and repeat. Otherwise you are doing optimization that may not make any difference. Without timing your code you don't know if code needs to be sped up even if you've sped up the code after doing your optimizing.

Others have mentioned loading the properties window in a separate thread. It's important to remember that Swing has only one thread called the EDT that does all of the painting of pixels to the screen. Any code that causes pixels on the screen to change should be called from the EDT and thus should not be called from a separate thread. So, if you have something that may take a while to run (perhaps a web service call or some expensive computation), you would launch a separate thread off of the EDT, and when it finishes run code on the EDT to do the UI update. There are libraries such as SwingWorker to make this easier. If you are setting a dialog to be visible, this should not be on a separate thread, but it may make sense to build the data structures in a separate thread if it is time consuming to build these data structures.

Using Swing Worker is one of many valuable ideas in Filthy Rich Clients for making UI's feel more responsive. Using the ideas in this book I have taken some fairly resource intensive UI's and made them so the UI was hardly using any resources at all.

Jay Askren
Agreed. @Arnon: I'd be very interested to hear what makes you think running two separate programs like you're proposing would make your program more efficient.
oltman
Thank you all.My Program is Filthy Rich Clients App.http://filthyrichclients.org/Which means a lot of system resources in use.My perception is that application should load fast, and respond to user action as fast as it can.Because the preferences window is not always used,i don't found a reason to load it when the program start - but just when it needs.That's why I want to separate them from each other.
Arnon
+1  A: 

Maybe you could use some sort of directory watcher like this or maybe implement some sort of semaphore. Honestly, I think that you should be able to solve the problem if you have some sort of menu item that the user can access. Once that the user saves the preferences, these are written to a file. The application then loads the values from the file whenever it needs them. If your system is operating slowly, or hanging, you might consider the use of threads, or increase the number of threads.

npinti
Great link to "Watching a Directory for Changes"! I didn't know that this was possible! I think this is the best solution to this question.
Jonas
I knew that there was something similar on C#. The great thing with Java and C# is that there is a decent amount of API's that do similar things :D
npinti
Pity it is java 7 - it will most likely take a couple of years before it becomes available on our target platform ...
Thorbjørn Ravn Andersen
A: 

Actually, as others have explained, you can use socket for inter-process communication. However, that won't reduce your overall CPU / RAM usage at all. (might even slightly worsen your resources usage)

For your case, you can launch the Perference window in a different Thread rather than a different Process. Thread is lighter for OS to handle and poses no additional complexity for inter-process communications.

dex
Swing is single threaded. Any code that causes pixels to change should be called from the Swing thread or EDT. You can't really run the main UI in one thread and the preferences UI in another thread. If you try, strange things can happen. See my answer.
Jay Askren
that's where SwingWorker comes in(as u have stated in ur ans). Obivously poster's problem doesn't come from UI updating but UI driven tasks. Normally Swing's buffer strategy works pretty good to avoid redundant repainting. So unless you are doing an over-complex anim loop, the GUI performance should be just fine.
dex
A: 

Nobody seems to have mentioned the DBUS - available to developers on a Linux system. I guess that's no good if you're trying to make a Windows/Cross Platform application, but the DBUS is a ready-made application-communication platform. It helps address issues such as:

  • Someone else might already be using the port you're trying. There's no way for you client application (The "Preferences" window I guess) to know whether the thing listening on that port is your main application, or just something else that happens to be there, so you'll have to do some sort of handshake, and implement a conflict-resolution mechanism
  • It's not going to be obvious to either the future you, or anyone who comes to maintain your app why you're on the port you are. This might not seem important, but communicating on Socket 5574 just doesn't seem as neat to me as communicating on channel org.yourorganisation.someapp .
  • Firewalls (as I think someone's already said) can be a little over-zealous

Also, it's worth getting your hand in on DBUS - it's useful for communicating with a whole bunch of other applications such as the little popup notification thing you'll find in recent Ubuntu distributions, or certain instant messaging clients, etc.


You can read up on what I'm talking about (and maybe correct me on some of the things I've said) here: http://www.freedesktop.org/wiki/Software/dbus . It looks like they're working on making it happen on Windows too, which is nice.

jelford