StringBuilder比String消耗更少的内存? [英] StringBuilder consumes less memory than String ?

查看:137
本文介绍了StringBuilder比String消耗更少的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StringBuilder比String消耗更少的内存?

解决方案

由于您未能提供有关您如何得出这个结论的任何上下文信息,我们只能说不 ,它不是。字符串是一种数据结构,它具有少量的头信息和字节数组(通常每个字符2个字节),用于保存字符串的数据。



A StringBuilder包含一个字符数组,并且有一堆与之关联的代码,因此,一般来说,StringBuilder不能占用比字符串更少的内存。



哦!当您实际将StringBuilkder转换为字符串以便可以使用它时,它会占用目标字符串的大约两倍的内存。一次保存字符串实例,另一个保存StringBuilder及其数据。


试试这个,



http://bit.ly/1dbelxh [ ^ ]

StringBuilder consumes less memory than String ?

解决方案

Since you failed to provide any context information about how you came to this conclusion, all we can say is "No, it does't". A string is a data structure that has a small amount of header information and array of bytes (usually 2 bytes per character) that hold the data for the string.

A StringBuilder holds an array of characters and has a bunch of code associated with it, so, in general, a StringBuilder cannot take up less memory than a string.

Oh! When you actually convert the StringBuilkder to a string so you can use it as such, it takes up about TWICE the memory of the target string. Once to hold the string instance and another to hold the StringBuilder and its data.


Try this,

http://bit.ly/1dbelxh[^]


这篇关于StringBuilder比String消耗更少的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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