Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey guys,
how can I use the maximum capacity of a stringbuilder on vb.net ???
strinbuilder's max capacitry is 2.147.483.647 and i can reach only 134.217.728 !
i've tried the EnsureCapacity method (
VB
Dim stra As New System.Text.StringBuilder
stra.EnsureCapacity(stra.MaxCapacity)
) but nothing happened !
I've also tried declaring the strinbuilder with capacity= maxcapacity (
Dim stra As New System.Text.StringBuilder(2147483647)
but still nothing !!!
help please !!!!

[edit]

I want to make a program and part of this program reads a file into a stringbuilder
if the file is 'bout of 1GB i can't read it in it !
anyway, Im not trying to reach Int32.MaxValue i just want it to read more than 134.217.728 bytes !
but im pretty new to memory management and i dont know how's that possible !
maybe with Marshal class ?

[/edit]
Posted
Updated 23-Jan-11 5:17am
v2
Comments
Estys 23-Jan-11 11:18am    
I inserted answer of OP into the original question.

Internally it needs a contiguous memory block, and that would be quite impossible to do if you want 2 GB. I would say even 1 GB would be a bit of a stretch. By the way if you need to do this, there's something wrong with your design.

BTW you are probably on a 32 bit machine. Going to 64 bit will give you a fairly good improvement in maximum capacity (if that's an option).
 
Share this answer
 
v2
Comments
Estys 23-Jan-11 11:18am    
OP added info.
Sergey Alexandrovich Kryukov 23-Jan-11 11:44am    
This is correct answer (my 5); your note on wrong design is important; I added some ideas on how to design things so make a working solution.
--SA
Why are you trying to do this? If you look at MSDN StringBuilder.MaxCapacity.aspx[^] is says that it is Int32.MaxValue already.

Setting the EnsureCapacity property only changes things if the new value is greater than the current Capacity value.

What did you think was going to happen? It allocates space, that is all. It doesn't fiull it up with anything useful (or useless for that matter). You can use the space, but why allocate that much space unless you are actually going to use it?
 
Share this answer
 
Comments
Estys 23-Jan-11 11:19am    
OP added info.
Sergey Alexandrovich Kryukov 23-Jan-11 11:45am    
This is correct answer (my 5); your "why" is important: things are probably ill-designed; I added some ideas on how to design things so make a working solution.
--SA
Whatever you try to squeeze from StringBuilder, it won't help you if you really approaching the memory limitation. As Nishant and Griff rightfully pointed out, the problem is your design.

It looks like the mere size of your file is already a stress for you memory if you try to keep it in memory at once. The techniques using such files are quite different. For one thing, you can use memory-mapped file instead, see http://en.wikipedia.org/wiki/Memory-mapped_file[^]. This is a relevant Microsoft help page with a sample to demonstrate the technique:

http://msdn.microsoft.com/en-us/library/dd997372.aspx[^].

From the other hand, the file remains unstructured to you, but you probably wanted to break the file into structured fragments, put each fragment to StringBuilder to have a random access to each fragment. For example, the fragment is the text-file line. As lines have different lengths, it is not possible to know the file position of each before you read the whole file.

The simplest popular technique is remembering index map of the file: you pre-read the whole file, but instead of putting all the content in memory, you just remember file position (which is 64-bit integer); for random access you would put those positions in a list. When this is done, you keep your file open. When you need a fragment itself, you find a file position by index, go to this position again and read appropriate amount of byte again. A modification of this technique is putting some extra information in the list: fragment length, classification, etc. You can use this technique with a regular file stream (with random access) or combine with memory-managed file.

I don't know what you're trying to achieve, but the above can merely give you the idea how to approach a big file.
Good luck!
--SA
 
Share this answer
 
v3
Comments
Nish Nishant 23-Jan-11 18:18pm    
Excellent info, voted 5.
[no name] 28-Jan-11 8:36am    
well, it seems I cant access the system.io.memmorymappedfile namespace !
if for example i type " <pre lang="vb.net"> Dim mmf as new System.IO.MemoryMappedFiles.Memorymappedfile </pre> I get an error saying that MemoryMappedFile cannot be found !
Even if I import the system.io.memorymappedfiles namespace i get a warning that the namespace cannot be found in consequence i get an error on "Dim mmf as new System.IO.MemoryMappedFiles.Memorymappedfile"

how can I solve that ???
if it helps , i have the same problem with system.directoryservices but when I "put" a directory searcher on the design page it gets fixxed !
any help ???
Sergey Alexandrovich Kryukov 28-Jan-11 12:00pm    
You .NET version, please...
--SA
[no name] 29-Jan-11 4:09am    
its 3.5
Sergey Alexandrovich Kryukov 30-Jan-11 0:14am    
Unfortunately, you need v.4.
Alternatively, the use of memory-mapped file could be made by P/Invoke.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900