Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

JDBC-connection

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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?


 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
soul reaver please check your private messages.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • 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:
http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html

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

 
There's a way to do it better - find it. -Edison. A better tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!