• Post Reply Bookmark Topic Watch Topic
  • New Topic

String[] how much memory.

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I want to know that if i use String[] a = new String[100], will it take any memory? If yes please tell me that how can i free it up. I also want to know that will it effect the performance if i use many times String[].

Thanks and regards
alexander
 
Evan Caballero
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, it will allocate an array object containing 100 null items.

to free it, you just have to do a = null; to dereference the array from its variable, and the garbage collector will do its job -> cleaning all unreferenced objects.

I'm not a "java memory" expert, but I think it works like this.

Why not using List<String> instead of String[] ?
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. when I did this experiment in the profiling tool JVisualVM,



it looks like the Arrays take the same amount of memory whether they are populated or not.

and the List barely registered. Like Evan Caballero says, use List<String> instead if you are concerned about memory.

attached is printscreen from jvisualVM:


visualvm.jpg
[Thumbnail for visualvm.jpg]
printscreen of visualVM
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for helping me out guyz.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!