Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven


Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
suppose we have a connection class ConnectionHelper, it contains methods for obtaining a connection and closing a connection

now we have method where connection from above class is used

now my question is when we say
getConnection of ConnectionHelper will be called,
now the drivermanager in connectionHelper will create a connection with db and the object that represents the
connection is stored at memory location say 0x1234
and the object "con1" of connectionHelper points to that location 0x1234
now in method userConnection()

hence con has a reference to con1 which actully refers to 0x1234 where actual connection information is stored.

now after closing the connection by calling

if we call userConnection() again
then since con1 is closed, it will create a new connection.
now this connection object must be somewhere in JVM heap memory , but this time when new object is created
will it again be created at same memory location or different???
if its stored at different location then what about the connection information at 0x1234?
and if every time new memory is used to store objects , won't the Heap memory get overflow?

Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
soul reaver please check your private messages.
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not really a JDBC question. While your example uses a JDBC-created object, the same theory applies to all created objects.

Java is a "garbage collecting" language; the memory space of any object that no longer has any "live" references may at any time be freed. This is managed by one or more threads running concurrently with your application and happens automatically from time to time. The garbage collector also automatically performs some compaction.

When a new object is being created and there isn't sufficient free memory on the heap, the garbage collector will first attempt to reap any dead objects, and if that fails will attempt to expand the heap - up to the maximum specified by the startup parameters on the java process (which may be a default value), and if that fails, will throw a fatal OutOfMemory exception.

Here's a nice introduction:

While there's lots and lots of things for Java gurus to know about garbage collection, at your current level I'd say:
1. don't keep unnecessary references to objects
2. make sure you explicitly close the JDBC resources you open or use a resource pool; in particular, if you expect to use JDBC on a real-world application in a concurrent use environment (such as a web application) you MUST know about connection pooling, whether you use it or not...
3. don't try to outsmart the garbage collector; that's a great way to really screw things up, until you're a real uber-guru
4. if you have any more questions about Java memory management, please move to a more appropriate forum

WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic