Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Random Access File Question  RSS feed

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all ,i was wondering that if i use a
RandomAccessFile to save data, should i
save empty objects (say 100,000 object)
and then for every object that i need to
insert ,i seek it's position and then modify
the value of the empty object.
that i have read in a book (Java2 how to program)
is there another way to use RandomAccessFile !
thx
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use of RandomAccessFile requires that you have some way to know exactly where each record starts. Frequently people do this by having a fixed record size and reserving space for a record by writing a zero filed record.

However - I am worried by your use of the term "objects" - with default Java object serialization, the size that is written depends on the state of the object and is very unlikely to remain a fixed record size.

If you want random access to objects, you will have to implement your own object serialization so that a fixed and predictable size is written.

Bill
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William
thx for replying ,all my objects have the same size
but my problem is that the myFile.dat wont contain
more than 300,000 record ,is it the limit?
and should i reserve 300,000 record is it practical?
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omar Bili,

What happens if you try to add more than 300,000 records to your data file? The only limit should be the amount of remaining physical disk space. I'm not sure if there's any advantage to creating blank records up front. Quite possibly if you want to maintain records in some order. Of course you'd then have to implement a strategy to fill in the gaps.

Jules
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was trying to insert 500,000 record
in a for loop.
then a message appeared : out of memory
i have 512 ram , and enough disk space
so i dont think it is the material !
and i will try to use a binary search on
my RandomAccessFile ,mybe i'll not be obliged
to reserve all the objects.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sounds like you might be holding onto a reference for all these objects in the VM and running out of memory in Java, nto space on disk. I have written binary files up to several GB's with no problems, just make sure there isn't a memory leak somewhere if it's looping over some big list.
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i am doing is:

//file is a RandomAccessFile
//Person is a serializable Object that i want to insert in file

String name = "xx" ;
int age = 0 ;

for (int = 0 ; i < 500000 ; i++) {

file.writeObject(new Person(name+i,i)) ;

}
maybe the objects are being created and not deleted !?
should i use the GarbageCollector , and how to call it
so plz tell me how to do delete these objects .
thx for helping me.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a memory leak in that code, but maybe you're not giving the GC a look-in; remember it's a low priority thread. You don't need to call it directly but you might help it along by adding something like the following to your loop:

HTH

Jules

[ August 19, 2004: Message edited by: Julian Kennedy ]
[ August 19, 2004: Message edited by: Julian Kennedy ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!