问题描述
我查看了java substring方法的所有内存泄漏解决方案。由于此问题,我仍然会出现内存不足错误。我有一个字符串的arraylist,长度为1000-3500。我索引它们并存储它们。问题是每个字符串都需要通过循环来运行,以存储相同字符串的所有可能不同长度。为此,我使用循环和子串方法。这种方法会导致内存泄漏问题。
我做过的sudo代码:
for(int i = 0; i< str.length; i ++)
{
//创建子串并将其索引为
str.substring(0,(str) 。长度() - i))的;
}
str:string。并且上面的循环运行直到arraylist中的所有字符串都被索引。我试图修复泄漏,
1.
for (int i = 0; i< str.length; i ++)
{
//创建子串并索引它
new String(str.substring(0,(str.length() - 一世)));
}
2.
for(int i = 0; i< str.length; i ++)
{
//创建子串并索引它
new String(str。 。SUBSTRING(0,(str.length() - i))的实习生());
}
3。
for(int i = 0; i< str.length; i ++)
{
//创建子串并索引它
new String(str。 。SUBSTRING(0,(str.length() - i))的)实习生();
}
我还有这个问题。我的java版本是:1.7.0_17。
编辑:
我明白这不是内存泄漏问题来自评论。我正在索引一些连续的字符串。比如说,
String s = abcdefghijkl;
我想将每个字符串索引为:
abcdefghjk
abcdefghj
abcdefhg
abcdefh
abcdef
abcde
abcd
..
..
a
为了执行此操作,我得到一个字符串,然后执行子串操作,获取该字符串并将其编入索引。
没有泄漏。
String 对象。如果
String
的长度为1000个字符,则表示您正在创建1000个对象。 是否真的需要创建这么多 String
对象?例如,是否可以使用 char []
来实现您的目标?
I have looked through all the memory leak solutions for java substring method. I still get the out of memory error due to this issue. I have an arraylist of string which are of length 1000-3500. i index them and store them. The issue is each string needs to be run through loop to store all possible varying lengths of same string. To do this, i use for loop and substring method. and this method causes the memory leak problem.
A sudo code of what i have done:
for(int i=0;i<str.length;i++)
{
//create substring and index it
str.substring(0,(str.length()-i));
}
str: string. and this above loops runs till all the string within the arraylist are indexed. I tried to fix the leak by,
1.
for(int i=0;i<str.length;i++)
{
//create substring and index it
new String(str.substring(0,(str.length()-i)));
}
2.
for(int i=0;i<str.length;i++)
{
//create substring and index it
new String(str.substring(0,(str.length()-i)).intern());
}
3.
for(int i=0;i<str.length;i++)
{
//create substring and index it
new String(str.substring(0,(str.length()-i))).intern();
}
Still i have the issue. My java version is: 1.7.0_17.
Edit:
I understand this is not a memory leak problem from the comments. I am indexing some continuous strings. Say for example,
String s= abcdefghijkl;
i want index each string as :
abcdefghjk
abcdefghj
abcdefhg
abcdefh
abcdef
abcde
abcd
..
..
a
To perform this,i get a string,then perform substring operation,get that string and index them.
There is no leak.
Please note that you're creating a huge amount of String
objects. If a String
has a length of 1000 characters you're creating 1000 objects.
Is it really needed to create so many String
objects? Would it be possible for example to use a char[]
to achive what you want?
这篇关于使用java substring方法的内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!