This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

String[] how much memory.

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[] ?
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for helping me out guyz.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic