vb.net如何达到stringbuilder的最大容量? [英] vb.net how can I reach maximum capacity of a stringbuilder ?

查看:122
本文介绍了vb.net如何达到stringbuilder的最大容量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
如何在vb.net上使用stringbuilder的最大容量?
strinbuilder的最大容量为2.147.483.647,而我只能达到134.217.728!
我已经尝试过SecureCapacity方法(

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 (

Dim stra As New System.Text.StringBuilder
stra.EnsureCapacity(stra.MaxCapacity)

),但是什么都没发生!
我也尝试过声明strinbuilder的容量为maxcapacity(

) but nothing happened !
I''ve also tried declaring the strinbuilder with capacity= maxcapacity (

Dim stra As New System.Text.StringBuilder(2147483647)

但还是一无所有!!!
请帮忙!!!!

[edit]

我想制作一个程序,该程序的一部分将文件读入stringbuilder
如果文件是"1GB左右",我无法在其中读取它!
无论如何,我不是想达到Int32.MaxValue我只希望它读取超过134.217.728字节!
但是我对内存管理来说还很陌生,我不知道这怎么可能!
也许有元帅课?

[/edit]

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]

推荐答案

内部,它需要一个连续的内存块,如果要2 GB的内存块就很难做到.我想说即使是1 GB也会有点麻烦.顺便说一句,如果您需要执行此操作,则您的设计有问题.

顺便说一句,您可能在32位计算机上.转到64位将使您在最大容量方面有了相当不错的改进(如果可以的话).
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).


为什么要尝试这样做?如果您查看 MSDN StringBuilder.MaxCapacity.aspx [ ^ ]表示它已经是Int32.MaxValue.

仅当新值大于当前Capacity值时,设置SecureCapacity属性才会更改内容.

您认为会发生什么?它分配空间,仅此而已.它不会用任何有用的东西(或对此无用的东西)来充实它.您可以使用空间,但是为什么要分配这么多的空间,除非您实际要使用它?
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?


无论您尝试从StringBuilder挤压什么,它都不会帮助您,您确实要达到内存限制.正如Nishant和Griff正确指出的那样,问题出在您的设计上.

如果您尝试一次将其保存在内存中,那么看起来文件的大小似乎已经成为内存的压力.使用此类文件的技术非常不同.一方面,您可以改用内存映射文件,请参见 http://en.wikipedia.org/wiki/Memory-映射文件 [ ^ ].这是一个相关的Microsoft帮助页面,其中包含用于演示该技术的示例:

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

另一方面,文件对您来说仍然是非结构化的,但是您可能想将文件分成结构化的片段,将每个片段放在StringBuilder中以对每个片段进行随机访问.例如,该片段是文本文件行.由于行的长度不同,因此在读取整个文件之前不可能知道每个文件的位置.

最简单的流行技术是记住文件的索引图:您预读了整个文件,但是您没有记住所有内容在内存中,而只是记住文件位置(它是64位整数).对于随机访问,您可以将这些位置放在列表中.完成后,将文件保持打开状态.当您本身需要片段时,可以按索引找到文件位置,然后再次转到该位置并再次读取适当的字节数.对该技术的一种修改是在列表中添加了一些其他信息:片段长度,分类等.您可以将此技术与常规文件流(具有随机访问权限)一起使用,或者与内存管理的文件结合使用.

我不知道您要达到什么目标,但是以上内容只能为您提供处理大型文件的想法.
祝你好运!
-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


这篇关于vb.net如何达到stringbuilder的最大容量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆