0

I have String[] inputMsg declared outside a loop and inside a loop I have inputMsg = new String[someSize]; Since the loop can loop lots of times, I find it out that creating new String[someSize] everytime realy wasteful so in the of each iteration of the loop I would like to remove that memory allocation or to delete it's content so the inputMsg will be null. How can it be done in Java?

1
  • creating an array of anything is going to have minimal impact on the amuont of memory allocated. Unless your array is 1000's long, it would be silly to worry about. Commented Oct 1, 2011 at 20:00

5 Answers 5

7

Why declare it outside a loop?

for (...) {
    String[] inputMsg = new String[someSize];
    ...more code...
}

The GC will take care of freeing the memory. If the GC turns out to be a bottleneck, and you have the numbers to prove it, then we can talk some more. But, it turns out most GCs are fairly efficient at reclaiming short-lived objects.

Sign up to request clarification or add additional context in comments.

2 Comments

The loop is inside a server side application that get's from the socket messages and each message I'm allocating new memory into "inputMsg"
If you're dealing with a server, and it's actually likely to be driven hard, you need to learn A LOT about the inner workings of Java heap management -- enough that you'd be telling us how things work rather than the other way around.
3

You don't delete objects in Java, you remove the references to them and wait for the garbage collector to collect them.

If you have a String[] (ie, an array of String) you can attempt to reuse it (especially if the old and new arrays are the same size), but that the best you're going to do.

If you want to be able to add things to an array (change its size) over time you shouldn't use a regular [] type array but should use a Vector or one of the other collection objects.

2 Comments

In most cases they won't be in the same size, since in the first iteration it can be of size 2 and in the second iteration it can be size 5... so that's why I want to get rid of the reference before I allocate new memory.
What about all the Strings in the array, or all the other Strings you create here and there? Are you deleting them??
2

You can't delete anything in java by hand. This is automatically done by the garbage collector. It runs from time to time or if more memory is needed. You can't influence it.

2 Comments

Well...you can influence it. System.gc()
Yes, but it would be utter foolishness to invoke GC after every String[] creation cycle. (And because it's so often misused it's often disabled.)
0

You should be able to force a garbage collect with System.gc() or Runtime.gc(). However, I strongly advise against it. Why do you want the memory freed quickly? Is there a specific reason?

3 Comments

each iteration of the loop is a new message I'm getting from the socket and in each iteration it can be in different sizes
System.gc() cannot force garbage collection, it can only suggest it.
Yeah, I'll be first to admit that I don't know an awful lot about Java. Since C# is a lot like Java, I find at least some C# knowledge is indirectly Java knowledge. In .NET GC.Collect() forces an async GC, and you can actually REALLY force it with GC.WaitForPendingFinalizers() after the GC.Collect call. (Actually, they do have some "abuse detection" in there that will suppress excessive GC's.) Note to self: "C#" != "Java" =)
0

Have you considered using a LinkedList instead of a String[]?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.