This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage collection and performance

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a lot of data in cache that gets updated frequently. Instead of dereferencing the object and creating a new instance and loading it all back into memory, I want to know if there is a way to slowly delete data on one thread while new data is added on another? Also, would this be positive or negative on garbage collection?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve, if by "dereferencing"* you mean removing references to an object by setting them to null or to some other object, that's really the only way to delete objects in Java. Then you wait for garbage collection to take effect. If you want to do that using multiple threads, that's certainly possible. E.g. one thread could slowly set references to null, and another thread could be loading new data. I don't see any particular benefit or problem here; I suppose it really depends on how you know when data may be safely removed. You could, for example, have a thread that just keeps scanning the data and removing anything that's older than a certain amount of time.

* Normally "dereference" means something else entirely - the act of using a reference to look up the data it refers to. In Java this is normally done implicitly, and we don't usually talk about it as it's the normal way references work in Java. But in C/C++ you would do it with the indirection operator, *, which is the opposite of the reference operator, &. Which doesn't really matter much to use here, except to say that dereferencing does not mean removing a reference.
[ June 12, 2007: Message edited by: Jim Yingst ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by "slowly" release data to GC. Caches are usually Maps (at least mine are) and you can simply do something like:

Making all the objects in the cache suddenly eligible for GC shouldn't have any terrible effects. It won't force GC to happen right away because, well, you pretty much can't do that at all.

Is there some reason to do something more complex?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a mention of SoftReference being used for this purpose.
pl. refer java.lang.ref.SoftReference and java.lang.ref.ReferenceQueue
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modern garbage collectors are optimized for the handling of short-lived objects. So creating new objects instead of caching them *can* actually have a positive effect on performance.

To give more concrete advice, I'd need to better understand what you are actually trying to accomplish...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic