• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tries and Freelists

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was wondering if it is possible to implement a freelist in Java? I guess it would have to work with or bypass the garbage collector in some way. Just curious, that's all.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's a freelist? Never heard of it.
You can disable garbage collection by retaining hard references to all objects; you doubtless know that already. Whether that is a good idea is a different matter.

Question too scary for this forum: moving to the “general” forum.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A freelist is used to allocate a pool of memory that can be recycled so memory doesn't need to be reallocated over and over again.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably can implement that, but you will still have garbage collection problems because all the objects removed from it will still exist in the heap. The concept behind Java® is that it uses an automatic heap.
If you had a reversible Turing machine, you could consider writing objects onto the tape and then winding the tape back when they are no longer required.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. Let me pick one up at Best Buy
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait a bit; they will come down by 25% after Christmas
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOL @Campbell
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I vaguely remember that there are Java caching frameworks which access a big lump of memory which is outside of the JVM's purview and use that for caching objects. I can't remember what technology they use to do that but probably if you poked around the documentation for some of them, you might find out.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Paul that seems to be exactly what I'm talking about.
I would like to pool memory so the garbage collector doesn't have as much work to do later on when the objects go out of scope.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a particular reason why you want to do that?
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest I'm not sure. The code I'm translating is C/C++ so I guess it is more appropriate to that. There are a lot of objects that would be going in and out of scope since I'm hotloading them according to the map. I just figured it would be a lot of work for the garbage collector and the pooled memory in the C/C++ code might work better. I'm not married to the idea, just wondering if it was possible.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you'd have to replicate what the garbage collector was doing, wouldn't you? Seems to me it's kind of like "I think my electricity bills are too high so I'll build a nuclear power plant in my basement and produce my own electricity."
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the objects go out of scope the garbage collector would take over imho. But if we are using metaphors, its more like we make too much trash, maybe we should recycle.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you could certainly try to reuse objects rather than letting them go out of scope. But that's extra work which I wouldn't bother doing until I found out that the garbage collection was costing too much.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. That's why I was trying to figure out how to pool
memory -- so I could reuse objects instead of relying on the garbage collector to allocate and discard and reallocate. If I could pool memory like I can in C/C++ I could reuse the objects that are generated fairly easily. I'm just trying to make things as efficient as possible. IMHO.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only problem with trying to pool memory like I was, was I was trying to do it using objects and downcasting them, which isn't possible (I was 99% sure of this but thought I'd post anyway)

So I was trying to do:
public void getNewObject()
{
...
Object t = new Object()
return t;
}
then returning from that method call:

String string = getNewObject()

Which clearly doesn't work.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops..that's supposed to be:

public Object getNewObject()
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At this point I'd say you haven't got the Java vocabulary to talk about what you want to do. I'd say your idea of pooling memory to save on garbage collection is way above your level right now.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are wrong. I've been programming in Java for years. I'm sorry but I take that as an insult.
 
Ted Gress
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
12 years to be exact.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ted Gress wrote:I think you are wrong. I've been programming in Java for years. I'm sorry but I take that as an insult.



If you take it as an insult then I apologize. I had no idea it was possible to program in Java for 12 years and not understand casting. But still, if you don't understand casting then I still think you aren't going to be able to write a caching system to avoid garbage collection.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

One way to do memory pooling is by using SoftReferences. They keep their target alive (i.e. not garbage-collected) as long as there is some other free heap space that the JVM can use. But if the JVM runs out of space, it will invalidate some objects pointed to by SoftReferences only. See the API documentation of the SoftReference class for more info.

When using SoftReferences, it is important to create a normal reference to the object when taking it to active use so that the target will not be garbage-collected when it is needed.

In practice, you cannot change the type of an object returned to the SoftReference pool, which limits reusability. And since garbage collection works rather nicely in modern-day Java, such pooling activities are usually needed only in special cases. For example, if you want to scan empirically how much free memory the JVM has but do not want to destabilize the system by OutOfMemoryErrors, you can create some data buffers pointed to by SoftReference objects and begin allocating memory until some of the objects pointed to by SoftReferences are taken by the garbage collector.

Vesa
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic