Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collection and performance  RSS feed

 
Steve Chambers
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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?
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!