-1

My java desktop application runs every time the computer starts up and runs as long as computer is ON. It monitors all the activities and interacts with database more often and will tell me some updates by looking on the internet at regular intervals etc..etc.. So what I usuallly do is creating the threads and calling the sleep method. BUt is there any way so that i can handle the memory more efficiently as my program runs 24/7. Are there any methods which will be very much useful if we want to make the program sit silently if there is no job to do . Any advice or suggestion will be helpful

Thanks in advance

Stunner
  • 694
  • 1
  • 13
  • 31
  • What's the point of using threads if they're only doing one task and then going inactive? – Cody Gray Mar 10 '13 at 10:23
  • dude..who said my thread is going to do only one task.. I said the thread is going to do task at regular intervals. Meanwhile in interval , it goes to sleep mode. Dont comment without reading and understanding the post fully – Stunner Mar 10 '13 at 10:51
  • I'm still puzzled about how an idle thread can possibly be consuming resources. They don't get time slices unless they're actually *doing* something, and generally if they're doing something, you want them to be doing that something. – Cody Gray Mar 10 '13 at 11:02

2 Answers2

0

Only allocate as much memory as you need, in other words, do not keep stuff that you do not need.

Also, run at lower priority; though with sleep-calls, this won't gain you much.

Of course you could, or depending on the kind of application, should, also quit the program after doing the job, and only let it restart by cron, anacron or similar services (depending on your operating system).

Sebastian Mach
  • 36,158
  • 4
  • 87
  • 126
  • hey @phresnel , i dot want to use third party applications. I just want my java program to run as a service which runs continously as long as system is on. – Stunner Mar 10 '13 at 10:54
  • @user1856744: `cron` and `anacron` are Linux/Unix basics, and even though they may not be part of the Kernel, they are still hardly described as "third party applications". – Sebastian Mach Mar 11 '13 at 09:07
  • then a) you should mention that fact, b) look at websearch->"cron alternative windows"->http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron – Sebastian Mach Mar 12 '13 at 16:23
  • what do you mean by "mention that fact" ?. I am using windows 7 and i dont like using that third party or such "cron alternatives". And I guess making the reference to null as soon as object is not used is the best solution for memory management – Stunner Mar 14 '13 at 13:16
  • By "mention that fact" I mean "mention that fact". Some questions need detail on the asker's environment. And as said, I wouldn't declare `cron` or windows task scheduler "third party", they can be seen a given by the operating system, like File IO, starting programs, managing memory. If your program for example only has to run once in an hour, it might be better to stuff it into a task scheduler, instead of wasting any resources. I can't guess all that from your question; ... – Sebastian Mach Mar 14 '13 at 14:19
  • ... your question needs more details: What do you mean with "activity" exactly? Sending mails? Mouse input? Operating system (this info still is not in the question)? Etc. The better the question, the better the answer. And, "not like" is not a good argument. – Sebastian Mach Mar 14 '13 at 14:20
0

I think you could use a Thread pool for your tasks, This hold some threads active and when one of your tasks needs a thread you can request one, this way you need less active trheads running

Oracle about thread pools

Edo Post
  • 672
  • 6
  • 18