• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static classes or non-static ?  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an application which runs once in a day and sleeps for the rest of the period.
I have written certain general utility classes which can be made static easily and can be shared amongst all the other classes.
But the disadvantage that I see here is that since my application sleeps for most of the time, these static objects would not be released and would be there all the time holding memory.
I was thinking of making these classes non-static and initializing them when needed and immediately releasing them after use.
Does this make sense ?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Celina,
yeah u sound right in trying to make ur static utilities as non-static and creating an object to access them as static things will remain in memory as along as JVM is alive (am i right people?) and it seems JVM is alive as you mentioned that it 'sleeps' rest of the day...
btw, if ur application requires invocation once a day then can't u use schedular on windows OR cron job on Unix to invoke ur application at a particular time once a day regularly...in that case u don't have to convert static objects to non-static ones...
regards
maulin
 
Celina Joseph
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maulin,
Thanks for the confirmation. Unfortunately I cannot use available schedulers because the time to run the application varies depending upon certain other variables.
 
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this really buy you anything? I can't recall seeing a Sun JVM shrink its amount of allocated memory, even if most or all of the Java heap is free.
- Peter
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at exactly where the memory is being consumed. If your class doesn't consume much memory, it doesn't matter. If it allocates some large objects to do its work, you might implement a "release" method which sets those objects to null and frees up whatever else needs to be freed until the next time your class is used.
This would save you the overhead of having to allocate and release the object in the code which invokes it, and would still keep your memory usage down when the application is idle.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!