K
Kevin
Hi guys,
how can we explain this that I just observed?
The task is to count the frequency of words in text. A simple change in
the code can result in HUGE memory usage difference.
The program goes like this:
The count data structure is a "Hashtable ht", it hashes String to myInt
(which is a tiny class to wrap a int).
The text is read in line by line, break into tokens of String[].
String oneLine = read in one text line.
String[] tokens = oneLine.split(MySpliterString);
while (tokens != null)
{
for (int i=0;i<tokens.length;i++)
{
myInt count = (myInt ) ht.get(tokens[index]);
if (count == null)
{
ht.put(new String(tokens[index]), new KDOneInt(1)); // use little
memory.
// ht.put(tokens[index], new KDOneInt(1)); // use A LOT OF
memory.
}
else
{
count.increaseValueByOne();
};
};
//
for (int cc = 0; cc<tokens.length; cc++)
{
tokens[cc] = null;
};
tokens = null;
oneLine = read in one text line.
tokens = oneLine.split(MySpliterString);
}
///////////
Now, comes the magic part:
if I use the line as above:
ht.put(new String(tokens[index]), new KDOneInt(1)); // use little
memory.
the program use little memory.
But if I use the line as above (commented out):
// ht.put(tokens[index], new KDOneInt(1)); // use A LOT OF
memory.
the program will use much much much more memory!
What is the point for that?
Thanks and have a nice day!
how can we explain this that I just observed?
The task is to count the frequency of words in text. A simple change in
the code can result in HUGE memory usage difference.
The program goes like this:
The count data structure is a "Hashtable ht", it hashes String to myInt
(which is a tiny class to wrap a int).
The text is read in line by line, break into tokens of String[].
String oneLine = read in one text line.
String[] tokens = oneLine.split(MySpliterString);
while (tokens != null)
{
for (int i=0;i<tokens.length;i++)
{
myInt count = (myInt ) ht.get(tokens[index]);
if (count == null)
{
ht.put(new String(tokens[index]), new KDOneInt(1)); // use little
memory.
// ht.put(tokens[index], new KDOneInt(1)); // use A LOT OF
memory.
}
else
{
count.increaseValueByOne();
};
};
//
for (int cc = 0; cc<tokens.length; cc++)
{
tokens[cc] = null;
};
tokens = null;
oneLine = read in one text line.
tokens = oneLine.split(MySpliterString);
}
///////////
Now, comes the magic part:
if I use the line as above:
ht.put(new String(tokens[index]), new KDOneInt(1)); // use little
memory.
the program use little memory.
But if I use the line as above (commented out):
// ht.put(tokens[index], new KDOneInt(1)); // use A LOT OF
memory.
the program will use much much much more memory!
What is the point for that?
Thanks and have a nice day!