Forums Register Login
Tries and Freelists

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.
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.
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.
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.
Yeah. Let me pick one up at Best Buy
Wait a bit; they will come down by 25% after Christmas
LOL @Campbell
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.
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.
Do you have a particular reason why you want to do that?
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.
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."
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.
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.
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.
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.
Oops..that's supposed to be:

public Object getNewObject()
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.
I think you are wrong. I've been programming in Java for years. I'm sorry but I take that as an insult.
12 years to be exact.

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.

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.

Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 454 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 18, 2018 22:12:25.