Large data amount management

Hi

I'm currently writing a Bittorrent Client (like Azureus, but simpler...) within the scope of a University project.

It is already up and runs quite well. The real problem by now is that it is very RAM consuming and I know what the problem is, which is quite a luck...

The program currently downloads piece of files in a random order from remote peers. At the end, it writes all these pieces into one or several files, according to a predefined order. The problem is that now, the program keeps all downloaded pieces into memory (into some objects).

For small files, this is not a pain while being not very efficient. But as soon as it deals with large files, the problem occurs:

OutOfMemoryException: java heap space

I know this is due to the JVM to run out of memory and that there is way to provide more memory to it, but this is not the way it should be resolved, since the program should be able to download huge files ( tens of Gigas) while the memory not being that much...

So my idea is to periodically write the data in a file (or maybe another thing, like a buffer), which is not held into physical memory. But I must be able to write data in a correct order, so maybe at the beginning of the file, in-between or at the end also...

Could anyone help me find the best way to do this and save this **** physical memory?

Thanks a lot...

Bat

[1416 byte] By [Bata] at [2007-11-26 15:28:06]
# 1

The segments you get of the final file are all numbered, right? You get a part of the file, and information about where it belongs in the final, complete file, right?

Can't you just create temporary files with that information in the filename?

I suppose another interesting option would be to use a database, and store the data as BLOBs. Maybe you could swap out different database implementations based on what works best. I'd just try the temp file route first, though.

paulcwa at 2007-7-8 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 2

I think random access files should be able to do this.. I haven't used them to write into yet, I've only read from them, but I think the API provides some things to be able to write wherever you want...

You'll need to know the length of the file for which you are downloading a part and exactly where that part fits into the file. With setLength you can set the length(obviously) and you can move around with seek(long)... If you play around with this you might work out something!

Message was edited by:

Peetzore

Peetzorea at 2007-7-8 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 3
Nice, randomaccessfile is just what I needed...And yes, I thought of creating a temp file for each piece, but I tried to find another (cleaner?) method, with not tons of temp files...But thanks a lotMessage was edited by: Bat
Bata at 2007-7-8 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 4
Did you get it to work? I'm curious for the reason of that exception... Looked for it but couldn't find it... Maybe wrong translation of your french error message. I tried "Invalid descriptor"
Peetzorea at 2007-7-8 21:44:09 > top of Java-index,Java Essentials,Java Programming...
# 5
The advantage of temp files is that they can survive across program restarts.The disadvantage is it makes for more resource management.
paulcwa at 2007-7-8 21:44:09 > top of Java-index,Java Essentials,Java Programming...