• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problems with thread exceeding cache size and garbage collection

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say that I am taking a number of threads off of a queue. My cache is an arbitrary size of 2000. Now the first thread is 1300 and the 2nd is 1200. Since the 2nd one is too big to go into the cache I get an exception. How, can I recover from this exception? Should I be doing garbage collection manually here? Sorry, if my question is hard to understand I haven't done too much work with threading before.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Cache of what? 2000 what? 1200 what? 1300 what?

Can you just catch the exception in your run() implementation, and deal with it?
 
William Ross
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really sure; I was just saying like arbitrary units. Say for Example 2000MB, 1300MB, and 1200MB. One of my office mates asked me this question to post it... I'll see if I can get more info about the problem. Thanks.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How, can I recover from this exception? Should I be doing garbage collection manually here?



The system will GC as it needs memory -- so if you get an out of memory error, all the garbage have been pretty much clean up.

Now... if the cache is just that -- a cache and not a critical datastore. You can rewrite the cache using weak references. This way, as the system needs memory, it can GC the cache as well.

Henry
 
William Ross
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not a critical datastore but I don't have control over the cache because it is being run in WebLogic. WebLogic is in control of the queueing of threads. The exception being thrown is a java.lang.OutOfMemoryError. Also, the reference is pulling in a buffered image. The garbage collector can't take the entire chunk back since it is one reference.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Henry]: so if you get an out of memory error, all the garbage have been pretty much clean up.

That's true at the point the exception was thrown. However at the point you catch the exception, you've already at least abandoned the attempted action which requred extra memory, and you may well have exited several other blocks and/or methods, making additional variables go out of scope, and allowing additional memory available to GC. Depending on what you're doing and how your code is organized, it may be perfectly reasonable to catch the exception and attemtp to proceed anyway. The critical question being - how important was the method call that threw the exception? Is it possible to proceed if that failed? Often the answer is no, but sometimes it's yes. Can't say much more in general - it all depends on the details of what one is doing here, I think.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You meant to say SoftReference Mr. Wong.
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mr. C Lamont Gilbert:
You meant to say SoftReference Mr. Wong.



This is actually a good point. Yes, I do mean the SoftReference class, but how do I reference (no pun intended) the package with something that can be googled? "weak references" seems to be a more commonly used term.

I don't like "references package". Maybe "java.lang.ref package" is better. Opinions appreciated...

Henry
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like this?

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ref/package-frame.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic