• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a 4GB string?  RSS feed

 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want make sure that my oracle CLOB is working so I want to create a 4GB string and persist it to the db and then retrieve it to make sure everything works. How do I create a 4GB string? (or at least close)


is this really correct???
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There are propably better ways. Remember that not all OS's and java editions support that large variables.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know that characters in Java are Unicode (UTF-16) and take up 16 bits, right? So should your "4 GB" string be two giga-characters? You did say you were putting it in a CLOB in Oracle, so the answer to that question might require finding out whether Oracle is using UTF-16 too, or something else.

Although I wouldn't worry too much about that. Personally for this test I would just keep creating larger strings until something broke, and then I would ask myself if my real data was ever going to reach that breaking point.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could also keep appending the string to itself, effectively doubling it each time, saving yourself a boatload of iterations...
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please correct me if I'm wrong, but.......

Because of immutability, don't you end up filling up the String pool unnecessarily in any of these ways? That's a huge amount of memory to use for a bunch of Strings you don't need.

My vote is to use a StringBuilder to create it, then use toString() to turn it into a String.

Ultimately, I agree with Paul. Decide what the real world usage max point would be, then test for that.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't end up filling the String pool, because those millions of Strings are removed by the garbage collection program.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You don't end up filling the String pool, because those millions of Strings are removed by the garbage collection program.


Are you sure?

During my studies for SCJP I have read that Strings are only eligible for GC based on whether or not you set them to null.

This site says it is implementation dependent.

Now I need to know
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice:

Well, it would seem to me that since you're not keeping millions of String references alive, those Strings in the pool would be available for GC.

John.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String concatenation is handled internally by a StringBuilder as of 1.6 (maybe 1.5 too; I don't recall), but this may also be implementation-dependent.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:Janeice:

Well, it would seem to me that since you're not keeping millions of String references alive, those String in the pool would be available for GC.

John.


The JavaRanch FAQ (scroll like 3/4 way down) says that even though you don't have an active reference to the String, the String is not eligible for GC. You need to set the String to null for GC to pick it up.

If I am misunderstanding this, I'm asking to be set straight.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:

The JavaRanch FAQ (scroll like 3/4 way down) says that even though you don't have an active reference to the String, the String is not eligible for GC. You need to set the String to null for GC to pick it up.

If I am misunderstanding this, I'm asking to be set straight.


How can you set a String in the pool to null? References get set to null. I wonder if the FAQ means that if you null out the reference, the String object gets made eligible for GC.

John.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/438724/Beginning-Java/java/Garbage-Collection-String

Joe Ess says in that thread that the String Pool isn't Garbage Collected....
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice:

If you look at Joe's response, String literals go into the pool, and are safe from GC, while runtime Strings can be GC'd. For example:

In the first example, I'm using a literal, so even if awesomeCoder goes out of scope, the String that it points to is in the pool. On the other hand, anotherString is created at runtime, so it can be GC'd if its ref is set to null, or it goes out of scope.

John.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the difference now, thanks John!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Although I wouldn't worry too much about that. Personally for this test I would just keep creating larger strings until something broke, and then I would ask myself if my real data was ever going to reach that breaking point.

I agree. However, Theodore: if it turns out to really be necessary to test using strings that are so large they exhaust the memory available... it may be advisable to stop using a String for this at all. If you've got an Oracle CLOB, you can use JDBC to write to it using a java.sql.Clob. The setCharacterStream method gives you a Writer that can write to the CLOB, without requiring that all the characters remain in the JVM memory.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should point out that a Java string is actually limited to 2^31 - 1 characters; any Java array is limited to this number of elements.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In theory, yes. In practice, you will run out of memory a long time before you hit that size. See my last post in this thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!