Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
Módulo C Python con uso de memoria inusual

Soy muy nuevo en Python C-API y en la creación de módulos. Traté de crear un módulo python c-hash. Uso python 3.4.3 y TDM-gcc (64 bits) 4.9.2 para la compilación en Windows.

Aquí mi código:

 // hash_mod.c #include <Python.h> unsigned long _hash(unsigned char const* str) { unsigned long hash = 5381; int c; int i; i = 0; while (str[i] != '\0') { c = str[i]; hash = ((hash << 5) + hash) + c; ++i; } return hash; } static PyObject* hash_hash(PyObject* self, PyObject* args) { unsigned char const* str; if (!PyArg_ParseTuple(args, "s", &str)) return NULL; return PyLong_FromUnsignedLong(_hash(str)); } static PyMethodDef HashMethods[] = { {"hash", hash_hash, METH_VARARGS, "String Hash"}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef HashModule = { PyModuleDef_HEAD_INIT, "hash", NULL, -1, HashMethods, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_hash(void) { return PyModule_Create(&HashModule); }

El archivo setup.py:

 # setup.py from distutils.core import setup, Extension module1 = Extension('hash', sources = ['hash_mod.c']) setup (name = 'Hash', version = '1.0', description = 'String Hash', ext_modules = [module1])

La compilación funciona bien, pero cuando trato de importar mi módulo hash en el intérprete, mi memoria da un gran salto, más de 2Go para el proceso python.exe .

Aquí hay una imagen de mi administrador de tareas que muestra el uso de la memoria:

  1. >>> import hash
  2. El final import hash
  3. Salir del intérprete de Python

Después de que finalice la importación, puedo usar mi módulo y funciona bien, pero la memoria parece un poco alta.

Me parece que PyModule_Create hace una asignación de memoria realmente grande. Pero estoy bastante seguro de que esto no sucede en otro módulo.

Me he perdido algo ?

Editar :

Cuando ya uso mucha RAM (más de 2.5Go/4Go), me sale este error:

 >>> import hash Traceback (most recent call last): File "<stdin>", line 1, in <module> MemoryError >>>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Como Python usa un recolector de basura, no necesita liberar memoria en ningún momento en particular y probablemente esté viendo la optimización en acción.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!